或者:
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.apache.commons.lang3.StringUtils;
public class MySMTP {
private static final String SMTP_SERVER = "smtp.unimarketing.org";
private static final String APIKey = "您的API Key";
private static final String APISecret = "您的API Secret";
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.put("mail.smtp.host", SMTP_SERVER);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "25");
properties.put("mail.transport.protocol", "smtp");
String fromAddress = "no-reply@example.com";
String recipient = "someone@customer.com";
String subject = "邮件主题";
String fileAttachment = "附件路径";
String content = "邮件正文内容";
InternetAddress[] receiveAddresses = new InternetAddress[] { new InternetAddress(recipient) };
Session session = Session.getInstance(properties, new SmtpAuth());
session.setDebug(true);
session.setDebugOut(new PrintStream("smtp.log"));
MimeMessage message = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
multipart.setSubType(ContentTypeEnum.ALTERNATIVE.getLabel());
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent(content, "text/html; charset=utf-8");
multipart.addBodyPart(bodyPart);
if (StringUtils.isNotBlank(fileAttachment)) {
DataSource dataSource = new FileDataSource(fileAttachment);
String name = dataSource.getName();
bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(dataSource));
bodyPart.setFileName(MimeUtility.encodeText(name));
multipart.addBodyPart(bodyPart);
}
message.setSubject(subject);
message.setContent(multipart);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, receiveAddresses);
InternetAddress[] addresses = new InternetAddress[1];
addresses[0] = new InternetAddress("someone@somewhere.com");
message.setReplyTo(addresses);
message.addHeader("X-List", MimeUtility.encodeText("联系人列表名称"));
message.addHeader("X-Campaign", MimeUtility.encodeText("邮件名称"));
Transport.send(message);
/**
* 获取返回信息
*/
File file = new File("smtp.log");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
if (tempString.lastIndexOf("250 ok:") != -1) {
System.out.println("返回数据为:" + tempString);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
System.out.println("发送成功");
}
static class SmtpAuth extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, PASSWORD);
}
}
}