java mail(发邮件)



package email;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
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.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;

/**
* 发送邮件原始代码
* @author wander
*
*/
public class SendEmailDemo1 {

public static void main(String[] args) throws Exception {
// send();
send2();

}

/**
* sun的mail.jar包
*/
public static void send() throws AddressException, MessagingException, ParseException, UnsupportedEncodingException {
String from = "**@163.com";
String to = "**@qq.com";
String host = "smtp.163.com";

Properties pro = System.getProperties();//new Properties();
pro.put("mail.smtp.host", host);
pro.put("mail.smtp.auth", "true");
pro.put("mail.smtp.port", 25);

//创建SESSION
Session session = Session.getDefaultInstance(pro, new Authenticator(){
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("wandao312", "");
}
});

//打印调试信息
session.setDebug(true);

//创建消息, 设置收信人, 主题
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));

//RecipientType.BCC:暗送, RecipientType.CC:抄送, RecipientType.TO:收件人
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject("邮件测试标题!", "UTF-8");//此处要编码转化, 不然会出显乱码
message.setText("邮件测试内容", "UTF-8");//纯文本邮件, 如果不是此类型, 则用setContent
message.setSentDate(new Date());

//带附件-----begin--------
Multipart multipart = new MimeMultipart();


MimeBodyPart messageBodyPart = new MimeBodyPart();
String fileName1 = "D:\\dd.sql";
// 得到数据源
FileDataSource fds = new FileDataSource(fileName1);
// BodyPart添加附件本身
messageBodyPart.setDataHandler(new DataHandler(fds));
// BodyPart添加附件文件名
messageBodyPart.setFileName(MimeUtility.encodeText("dd.sql"));
multipart.addBodyPart(messageBodyPart);

//当有附件时设置邮件内容, 如果带附件, 则要再次设置内容, 不然像上面的setText是不起作用
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setContent("邮件测试内容1", "text/html;charset=UTF-8");
multipart.addBodyPart(mbp1);

//当有附件时设置邮件内容, 当再次设置内容时, 会把第二次的当成附件
// MimeBodyPart mbp2 = new MimeBodyPart();
// mbp2.setContent("邮件测试内容2", "text/html;charset=UTF-8");
// multipart.addBodyPart(mbp2);

message.setContent(multipart);
// -----end--------

Transport.send(message);
// Transport transport = session.getTransport("smtp");
// transport.connect("name", "");
// transport.send(message);
// transport.close();
}

/**
* APACHE的common-email.jar包.
* @throws EmailException
*/
public static void send2() throws EmailException {
//不带附件 ------begin------
Email email=null;
email=new HtmlEmail();
email.setDebug(true);
email.setHostName("smtp.163.com");
email.setSmtpPort(25);
email.setAuthentication("wandao312","");
email.setSubject("test");
email.setCharset("gbk");
email.setFrom("**@163.com");
email.addTo("**@qq.com");
email.setMsg("邮件测试4");

email.send();
// ------end------


//带附件 ------begin------
/*Email multiPartEmail = new HtmlEmail();
multiPartEmail.setDebug(true);
multiPartEmail.setHostName("smtp.163.com");
multiPartEmail.setSmtpPort(25);
multiPartEmail.setAuthentication("name","");
multiPartEmail.setSubject("test");
multiPartEmail.setCharset("gbk");
multiPartEmail.setFrom("**@163.com");
multiPartEmail.addTo("**@qq.com");*/

/*EmailAttachment ea = new EmailAttachment();
ea.setPath("D:\\dd.sql");

multiPartEmail.attach(ea);*/

// multiPartEmail.send();

// ------end------
}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值