java mail 实例1

package test;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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;

public class SendMail {
private MimeMessage mimeMsg;

private Session session;

private Properties props;

private boolean needAuth = false; // stmp need authorize

private String username = "";

private String password = "";

private Multipart mp;

public SendMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}

public void setSmtpHost(String hostName) {
if (props == null)
props = System.getProperties();
props.put("mail.smtp.host", hostName);
}

public boolean createMimeMessage() {
try {
session = Session.getDefaultInstance(props, null);
} catch (Exception e) {
e.printStackTrace();
return false;
}
try {
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public void setNeedAuth(boolean need) {
if (props == null)
props = System.getProperties();

if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}

public void setNamePass(String name, String pass) {
username = name;
password = pass;
}

public boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean setBody(String mailBody) {
BodyPart bp = new MimeBodyPart();
try {
bp.setContent("<html><head><meta http-equiv=Content-Type content=text/html; charset=utf-8></head><body>" + mailBody + "</body></html>",
"text/html;charset=utf-8");
mp.addBodyPart(bp);
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}

public boolean addFileAffix(String filename) {
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());

mp.addBodyPart(bp);

return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean setTo(String to) {
if (to == null)
return false;

try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
}
}

public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC, (Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}

public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();

Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username, password);
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public static void main(String[] args) {
String mailbody = "<div align=center><a href=\"https://www.163.com\">this is a test</a></div>";

SendMail themail = new SendMail("smtp.163.com");
themail.setNeedAuth(false);

if (themail.setSubject("this is test") == false)
return;
if (themail.setBody(mailbody) == false)
return;
if (themail.setTo("test@163.com") == false)
return;
if (themail.setFrom("\"test111\"<test111@163.com>") == false)
return;

if (themail.sendout() == false)
return;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值