英文:
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 = "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...");
}
答案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 = "Rabin Samanta <rabin.samanta@xxx.com>";
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 <user@host.domain>".
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("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);
}
And check the SMTP Bucket https://www.smtpbucket.com/emails?sender=me@example.com
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论