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------
}
}
java mail(发邮件)
最新推荐文章于 2023-06-24 17:57:09 发布