Java -发送邮件 email

时间:2022-03-17 21:11:29

Java 发送邮件

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;
import javax.mail.internet.MimeUtility;


public class SendEmailBean {
// 用户Id
private String filename;
// 邮件服务器

private String mailServer;
// 发件人地址
private String emailfrom;
// 收件人地址
private String emailto;
// 抄送人地址
private String emailcc;
// bcc地址
private String emailbcc;
// 邮件
private String title;
// 邮件附件服务器上的保存地址
private String fujianAddress;
// 邮件正文
private String content;

/**
* 发送邮件的方法
*
* @param bean
* @throws Exception
*/

@SuppressWarnings("static-access")
public static void send(SendEmailBean bean) throws Exception {
try {
// 建立邮件会话
Properties props = new Properties(); // 用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,

// 存储发送邮件服务器的信息

props.put("mail.smtp.host", bean.getMailServer());
// 同时通过验证
props.put("mail.smtp.auth", "false");
// 根据属性新建一个邮件会话

Session s = Session.getInstance(props);

// 由邮件会话新建一个消息对象

MimeMessage message = new MimeMessage(s);

// 设置邮件
if (bean.getEmailfrom() != null && bean.getEmailfrom().length() > 0) {
InternetAddress from = new InternetAddress(bean.getEmailfrom());
message.setFrom(from); // 设置发件人的地址
}
// 设置发送人
String str_to = bean.getEmailto();
if (str_to != null && str_to.length() != 0) {
String[] sendto = str_to.trim().split(";");
InternetAddress[] to = new InternetAddress[sendto.length];
for (int i = 0; i < to.length; i++) {
to[i] = new InternetAddress(sendto[i]);
}
// //设置收件人,并设置其接收类型为TO
message.setRecipients(Message.RecipientType.TO, to);
}
// 设置抄送人
String str_cc = bean.getEmailcc();
if (str_cc != null && str_cc.length() != 0) {
String[] sendcc = str_cc.trim().split(";");
InternetAddress[] cc = new InternetAddress[sendcc.length];
for (int i = 0; i < cc.length; i++) {
cc[i] = new InternetAddress(sendcc[i]);
}
// //设置收件人,并设置其接收类型为CC
message.setRecipients(Message.RecipientType.CC, cc);
}
// 设置BCC人

String str_bcc = bean.getEmailbcc();
if (str_bcc != null && str_bcc.length() != 0) {
String[] sendbcc = str_bcc.trim().split(";");
InternetAddress[] bcc = new InternetAddress[sendbcc.length];
for (int i = 0; i < bcc.length; i++) {
bcc[i] = new InternetAddress(sendbcc[i]);
}
// //设置收件人,并设置其接收类型为CC
message.setRecipients(Message.RecipientType.BCC, bcc);
}
// 设置标题
if (bean.getTitle() != null && bean.getTitle().length() > 0) {
String title =MimeUtility.encodeText( bean.getTitle(),"UTF-8","B");
message.setSubject(title);
}

Multipart multipart = new MimeMultipart();// 附件传输格式

if (bean.getContent() != null && bean.getContent().length() > 0) {
BodyPart textBodyPart = new MimeBodyPart(); // 第一个BodyPart.主要写一些一般的信件内容。

textBodyPart.setContent(bean.getContent(),
"text/html;charset=UTF-8");

// textBodyPart.setText(bean.getContent());
multipart.addBodyPart(textBodyPart);

}
if (bean.getFujianAddress() != null
&& bean.getFujianAddress().length() > 0) {
// BodyPart设置
FileDataSource fds = new FileDataSource(bean.getFujianAddress());
BodyPart fileBodyPart = new MimeBodyPart(); // 创建BodyPart
fileBodyPart.setDataHandler(new DataHandler(fds)); // 字符流形式装入文件

if (bean.getFilename() != null
&& bean.getFilename().length() > 0) {
fileBodyPart.setFileName(bean.getFilename());
} else {
fileBodyPart.setFileName(fds.getName());
}
multipart.addBodyPart(fileBodyPart);

}
message.setContent(multipart);
// 设置发信时间
message.setSentDate(new Date());
// 存储邮件信息
message.saveChanges();
// 发送邮件

Transport transport = s.getTransport("smtp");
// 以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect();
// 发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message, message.getAllRecipients());
transport.close();

} catch (Exception e) {
e.printStackTrace();
}
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public String getMailServer() {
return mailServer;
}

public void setMailServer(String mailServer) {
this.mailServer = mailServer;
}

public String getEmailfrom() {
return emailfrom;
}

public void setEmailfrom(String emailfrom) {
this.emailfrom = emailfrom;
}

public String getEmailto() {
return emailto;
}

public void setEmailto(String emailto) {
this.emailto = emailto;
}

public String getEmailcc() {
return emailcc;
}

public void setEmailcc(String emailcc) {
this.emailcc = emailcc;
}

public String getEmailbcc() {
return emailbcc;
}

public void setEmailbcc(String emailbcc) {
this.emailbcc = emailbcc;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getFujianAddress() {
return fujianAddress;
}

public void setFujianAddress(String fujianAddress) {
this.fujianAddress = fujianAddress;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

另外,可以参考:

http://blog.csdn.net/xietansheng/article/details/51673073