我想在发送电子邮件时创建一个新的自定义标头,但是通过使用setHeader()
和addHeader()
方法我无法做到。
如何创建用户定义的X-""
电子邮件标头?
我想在发送电子邮件时创建一个新的自定义标头,但是通过使用setHeader()
和addHeader()
方法我无法做到。
如何创建用户定义的X-""
电子邮件标头?
setHeader() 对我有用。在以下情况下,我将编码选项设置为邮件标题。
String mail_body = "<html><head></head><body><h1>Mail Body</h1><b>This is mail body of Test mail.</b></body></html>";
String encodingOptions = "text/html; charset=UTF-8";
MimeMessage message = new MimeMessage(session);
message.setContent("Hello", "text/plain");
message.setSubject(mail_subject);
message.setText(mail_body);
message.setHeader("Content-Type", encodingOptions);
message.setSentDate(new Date());
您可以在使用https://support.google.com/mail/bin/answer.py?hl=en&answer=22454接收电子邮件时查看邮件标题
示例程序。邮件.java
package com.test.mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail {
private String host = "smtp.gmail.com";
private String mail_to = "xxxx@gmail.com";
private String mail_from = "aaaa@abc.com";// using gmail server
private String mail_subject = "Subject of this test mail";
private String mail_body = "<html><head></head><body><h1>Mail Body</h1><b>This is mail body of Test mail.</b></body></html>";
private String personalName = "xxx";
private String encodingOptions = "text/html; charset=UTF-8";
public void sendMail() throws SendFailedException {
try {
Properties props = new Properties();
Authenticator auth = new Email_Autherticator();
//props.put("mail.smtp.host", host);
props.put("mail.smtp.host", host);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
System.out.println(props);
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setContent("Hello", "text/plain");
message.setSubject(mail_subject);
message.setText(mail_body);
message.setHeader("Content-Type", encodingOptions);
//message.setHeader(mail_head_name, mail_head_value);
message.setSentDate(new Date());
Address address = new InternetAddress(mail_from, personalName);
message.setFrom(address);
Address toaddress = new InternetAddress(mail_to);
message.addRecipient(Message.RecipientType.TO, toaddress);
System.out.println(message);
Transport.send(message);
System.out.println("Send Mail Ok!");
} catch (Exception e) {
e.printStackTrace();
}
// return flag;
}
public static void main(String args[]) {
try {
Mail mailObj = new Mail();
mailObj.sendMail();
} catch (Exception e) {
}
}
}
Email_Autherticator.java
package com.test.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
String username = "froml@gmail.com";
String password = "password";
public Email_Autherticator() {
super();
}
public Email_Autherticator(String user, String pwd) {
super();
username = user;
password = pwd;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}