Add title in send mail inside from.as like "Stack Overflow <do-not-reply@stackoverflow.email>"

huangapple go评论83阅读模式
英文:

Add title in send mail inside from.as like "Stack Overflow <do-not-reply@stackoverflow.email>"

问题

以下是翻译好的代码部分:

String to = "rabin.samanta@xxx.com";
String from = "rabin.samanta@xxx.com";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("username", "password");
    }
});
String msgBody = "test............";
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from, "NoReply"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, "Mr. Recipient"));
msg.setSubject("Welcome To Java Mail API");
msg.setText(msgBody);
msg.setHeader("header_name", "header_value");
Transport.send(msg);
System.out.println("Email sent successfully...");
英文:

I am not able to send mail to adding titles with from tab.
I need to add the title and text with the mail as like "Stack Overflow <do-not-reply@stackoverflow.email>"
How I will add the Stack Overflow title font of mail id.

My code is adding bellow

			String to = &quot;rabin.samanta@xxx.com&quot;;
		String from = &quot;rabin.samanta@xxx.com&quot;;
		Properties props = new Properties();
		props.put(&quot;mail.smtp.host&quot;, &quot;smtp-mail.outlook.com&quot;);
		props.put(&quot;mail.smtp.port&quot;, &quot;587&quot;);
		props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
		props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
		Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(&quot;username&quot;, &quot;password&quot;);
			}
		});
		String msgBody = &quot;test............&quot;;
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from, &quot;NoReply&quot;));
		msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to, &quot;Mr. Recipient&quot;));
		msg.setSubject(&quot;Welcome To Java Mail API&quot;);
		msg.setText(msgBody);
		msg.setHeader(&quot;header_name&quot;, &quot;header_value&quot;);
		Transport.send(msg);
		System.out.println(&quot;Email sent successfully...&quot;);
	}

答案1

得分: 0

你可以简单地使用以下代码:

String from = "Rabin Samanta <rabin.samanta@xxx.com>";

InternetAddress 类文档中:

此类使用 RFC822 语法表示互联网电子邮件地址。典型的地址语法为 "user@host.domain" 或 "Personal Name user@host.domain"。

https://docs.oracle.com/javaee/6/api/index.html?javax/mail/internet/InternetAddress.html


当你尝试运行以下代码时会发生什么:

public static void main(String... args) throws Exception {
    Properties props = new Properties();
    props.put("mail.debug", "true");
    props.put("mail.smtp.host", "mail.smtpbucket.com");
    props.put("mail.smtp.port", "8025");
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.auth", "false");

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setJavaMailProperties(props);

    MimeMessage msg1 = javaMailSender.createMimeMessage();
    MimeMessageHelper msg = new MimeMessageHelper(msg1, true);
    String from= "Me <me@example.com>";
    msg.setFrom(new InternetAddress(from));
    msg.setTo("Me <me@example.com>");
    msg.setText("Hello");

    javaMailSender.send(msg1);
}

并且检查 SMTP Bucket https://www.smtpbucket.com/emails?sender=me@example.com

英文:

You can simply use String from = &quot;Rabin Samanta &lt;rabin.samanta@xxx.com&gt;&quot;;

From the InternetAddress class documentation

> This class represents an Internet email address using the syntax of RFC822. Typical address syntax is of the form "user@host.domain" or "Personal Name &lt;user@host.domain&gt;".

https://docs.oracle.com/javaee/6/api/index.html?javax/mail/internet/InternetAddress.html


What happens when you try this

public static void main(String... args) throws Exception {
    Properties props = new Properties();
    props.put(&quot;mail.debug&quot;, &quot;true&quot;);
    props.put(&quot;mail.smtp.host&quot;, &quot;mail.smtpbucket.com&quot;);
    props.put(&quot;mail.smtp.port&quot;, &quot;8025&quot;);
    props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;false&quot;);
    props.put(&quot;mail.smtp.auth&quot;, &quot;false&quot;);

    JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
    javaMailSender.setJavaMailProperties(props);

    MimeMessage msg1 = javaMailSender.createMimeMessage();
    MimeMessageHelper msg = new MimeMessageHelper(msg1, true);
    String from= &quot;Me &lt;me@example.com&gt;&quot;;
    msg.setFrom(new InternetAddress(from));
    msg.setTo(&quot;Me &lt;me@example.com&gt;&quot;);
    msg.setText(&quot;Hello&quot;);

    javaMailSender.send(msg1);
}

And check the SMTP Bucket https://www.smtpbucket.com/emails?sender=me@example.com

huangapple
  • 本文由 发表于 2020年10月19日 17:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64424543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定