java 发送邮件 简单易操作


/**
* Created by IntelliJ IDEA.
* User: tsaowe
* Date: 12-3-6
* Time: 上午9:15
*/

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

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
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;

public class MailSender {

private String host;
private Address from;
private String username;
private String password;
private Address[] to;

private String subject;
private String message;

public MailSender() {
/* todo 这儿是发送邮件的服务器 */
this.host = "smtp.????.???";
try {
/* todo 这儿填写发给别人的邮件地址,就是自己邮箱地址*/
this.from = new InternetAddress("xxxx@xx.com");
} catch (AddressException e) {
e.printStackTrace();
}
this.username = "xxxx@xx.com"; //登录名,和上面邮件地址一致
this.password = "xxxxxxxxxx"; //登录密码

this.subject = "";
this.message = "";
}

public MailSender(String host, String from, String username, String password){
this.host = host;
try {
this.from = new InternetAddress(from);
} catch (AddressException e) {
e.printStackTrace();
}
this.username = username;
this.password = password;

this.subject = "";
this.message = "";
}

public void addAddress(String to) {
int length = 0;
if(this.to == null || this.to.length == 0){
length = 0;
}else{
length = this.to.length;
}
Address[] oldTo = this.to;
this.to = new InternetAddress[length + 1];
try {
this.to[0] = new InternetAddress(to);
} catch (AddressException e) {
e.printStackTrace();
}

for(int i = 0; i<length; i++) {
assert oldTo != null;
this.to[i+1] = oldTo[i];
}

}

public void setSubject(String subject) {
this.subject = subject;
}


public void setMessage(String message) {
this.message = message;
}

public String getMessage() {
return this.message;
}

public boolean sendMail() {
Properties props = new Properties();
props.put("mail.smtp.host", this.host);
props.put("mail.smtp.auth", "true");

Session session = Session.getInstance(props, null);

try {

Message msg = new MimeMessage(session);
msg.setFrom(this.from);
Address[] address = this.to;
msg.setRecipients(Message.RecipientType.TO, address);
((MimeMessage)msg).setSubject(this.subject,"utf-8");
msg.setSentDate(new Date());

BodyPart mdp=new MimeBodyPart();
mdp.setContent(this.message,"text/html;charset=utf-8");
Multipart mm=new MimeMultipart();

mm.addBodyPart(mdp);


msg.setContent(mm);
msg.saveChanges();

Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

System.out.println("Send mail: "+ msg.getSentDate());

return true;

} catch (MessagingException mex) {
System.out.println("\n--Exception");

System.out.println();
Exception ex = mex;
do {
if (ex instanceof SendFailedException) {
SendFailedException sfex = (SendFailedException)ex;
Address[] invalid = sfex.getInvalidAddresses();
if (invalid != null) {
System.out.println(" ** Invalid Addresses");
for (Address anInvalid : invalid) System.out.println(" " + anInvalid);
}
Address[] validUnsent = sfex.getValidUnsentAddresses();
if (validUnsent != null) {
System.out.println(" ** ValidUnsent Addresses");
for (Address aValidUnsent : validUnsent) System.out.println(" " + aValidUnsent);
}
Address[] validSent = sfex.getValidSentAddresses();
if (validSent != null) {
System.out.println(" ** ValidSent Addresses");
for (Address aValidSent : validSent) System.out.println(" " + aValidSent);
}
}
System.out.println();
if (ex instanceof MessagingException)
ex = ((MessagingException)ex).getNextException();
else
ex = null;
} while (ex != null);

return false;
}
}

public static void main(String[] args) {
MailSender mailSenderInstance = new MailSender();
mailSenderInstance.addAddress("xxxx@xxx.xxx");
mailSenderInstance.setSubject("Test Mail");
mailSenderInstance.setMessage("Test Mail");
mailSenderInstance.sendMail();

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值