标题:
java发送邮件(1)
[打印本页]
作者:
look_w
时间:
2019-4-20 09:20
标题:
java发送邮件(1)
运用例子如下:
package com.util.mail;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailUtil extends Thread {
private static int port = 25;
private static String server = "smtp.sina.com";// 邮件服务器mail.cpip.net.cn
private static String from = "zoe";// 发送者,显示的发件人名字
private static String user = "xxxxxx@sina.com";// 发送者邮箱地址
private static String password = "xxxxxx";// 密码
private List<String> emails;
private String subject;
private List<String> messages;
public MailUtil(List<String>emails, String subject, List<String> messages) {
this.emails = emails;
this.subject = subject;
this.messages = messages;
}
public void run() {
try {
sendEmail();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
private Multipart parseMessage() throws MessagingException {
Multipart mp = new MimeMultipart("related");
StringBuilder sb = new StringBuilder();
sb.append("<html><body><P>邮件信息</p><P>");
// sb.append(nickName);
sb.append("! 您好:</p>");
sb.append("<P> 以下是今天的信息</P>");
sb.append("---------------------------------------------------------------------------》");
for (String message : messages) {
sb.append("<p>" + message + "</p>");
}
sb.append("<p></P>");
sb.append("---------------------------------------------------------------------------》");
sb.append("<P> 竭诚为您服务,感谢您对我们的支持!</P>");
sb.append("<P> 我们的网址: <a href='http://blog.csdn.net/zzq900503'>白夜博客</a> </P>");
sb.append("<P> 此邮件为系统自动发出,请勿直接回复。</P>");
sb.append("</body></html>");
BodyPart bodyPart = new MimeBodyPart();// 正文
bodyPart.setDataHandler(new DataHandler(sb.toString(),
"text/html;charset=UTF-8"));// 网页格式
mp.addBodyPart(bodyPart);
return mp;
}
private void sendEmail() throws UnsupportedEncodingException {
try {
Properties props = new Properties();
props.put("mail.smtp.host", server);
props.put("mail.smtp.port", String.valueOf(port));
props.put("mail.smtp.auth", "true");
Transport transport = null;
Session session = Session.getDefaultInstance(props, null);
transport = session.getTransport("smtp");
transport.connect(server, user, password);
MimeMessage msg = new MimeMessage(session);
msg.setSentDate(new Date());
InternetAddress fromAddress = new InternetAddress(user, from,
"UTF-8");
msg.setFrom(fromAddress);
InternetAddress[] toAddress = new InternetAddress[emails.size()];
for(int i = 0; i<emails.size();i++){
toAddress[i] = new InternetAddress(emails.get(i));
}
msg.setRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject, "UTF-8");
msg.setContent(parseMessage());
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<String> emails = new ArrayList<String>();
emails.add("12345645@qq.com");
List<String> messages = new ArrayList<String>();
messages.add("aaa");
MailUtil mu = new MailUtil(emails, "aa", messages); //发送到的邮箱地址,对方称呼,发送信息
mu.start();
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0