资源简介
1、实现java发送邮件,支持文本发送,带附件发送;
2、解压文件,导入即可使用。(邮箱账号替换为自己的邮箱账号即可)。
代码片段和文件信息
package com.cn;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
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;
import javax.mail.internet.MimeUtility;
public class MailUtil {
public static Properties prop = new Properties();
private static Session session = null ;
private static Message msg;
private static Transport transport;
private static String sender_username = “cattznwg@163.com“;
private static String sender_password = “Cat2016“;
private static String port = “25“;
private static String host = “smtp.163.com“;
public static void init(){
prop.put(“address“ sender_username);
prop.put(“password“ sender_password);
prop.put(“mail.smtp.port“ port);
prop.put( “mail.smtp.host“ host);
prop.put(“mail.smtp.auth“ “true“);
prop.put(“mail.transport.protocol“ “smtp“);
session = Session.getInstance(prop);
}
public static void main(String[] args) throws UnsupportedEncodingException MessagingException {
prop.put(“address“ sender_username);
prop.put(“password“ sender_password);
prop.put(“mail.smtp.port“ 25);
prop.put( “mail.smtp.host“ “smtp.gdcattsoft.com“);
prop.put(“mail.smtp.auth“ “true“);
prop.put(“mail.transport.protocol“ “smtp“);
session = Session.getInstance(prop);
// 打开调试,会打印与邮箱服务器回话的内容
session.setDebug(true);
Message message = new MimeMessage(session);
// 如果发送人没有写对,那么会出现 javamail 550 Invalid User
// 如果发送人写的和使用的帐号不一致,则会出现 553 Mail from must equal authorized user
InternetAddress from = new InternetAddress(“lizhizhi@gdcattsoft.com“);
from.setPersonal(MimeUtility.encodeText(“java“));
message.setFrom(from);
InternetAddress to = new InternetAddress(“lizhizhi@gdcattsoft.com“);
message.setRecipient(Message.RecipientType.TO to);
message.setSubject(MimeUtility.encodeText(“1111“));
message.setText(“22222“);
message.setSentDate(new Date());
Transport transport = session.getTransport(“smtp“);
// 具体你使用邮箱的smtp地址和端口,应该到邮箱里面查看,如果使用了SSL,网易的端口应该是 465/994
transport.connect(“smtp.gdcattsoft.com“ 25 “lizhizhi@gdcattsoft.com“ “aaaaaaaaa“);
transport.sendMessage(message message.getAllRecipients());
transport.close();
System.out.println(“发送完毕“);
}
/**
* 发送邮件
*/
public static Boolean sendMail(String subject String sendHtmlList
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-16 16:31 sendEmail_demo\
文件 358 2017-12-16 16:33 sendEmail_demo\.classpath
文件 390 2017-12-16 16:29 sendEmail_demo\.project
目录 0 2017-12-16 16:29 sendEmail_demo\.settings\
文件 598 2017-12-16 16:29 sendEmail_demo\.settings\org.eclipse.jdt.core.prefs
目录 0 2017-12-16 16:33 sendEmail_demo\bin\
目录 0 2017-12-16 16:33 sendEmail_demo\bin\com\
目录 0 2017-12-16 16:35 sendEmail_demo\bin\com\cn\
文件 6056 2017-12-16 16:34 sendEmail_demo\bin\com\cn\MailUtil.class
文件 3900 2017-12-16 16:38 sendEmail_demo\bin\com\cn\MailUtil2.class
文件 415 2017-12-16 16:39 sendEmail_demo\bin\com\cn\Test.class
目录 0 2017-12-16 16:31 sendEmail_demo\lib\
文件 653275 2017-12-16 16:31 sendEmail_demo\lib\javax.mail.jar
目录 0 2017-12-16 16:30 sendEmail_demo\src\
目录 0 2017-12-16 16:30 sendEmail_demo\src\com\
目录 0 2017-12-16 16:35 sendEmail_demo\src\com\cn\
文件 5258 2017-12-16 16:34 sendEmail_demo\src\com\cn\MailUtil.java
文件 9525 2017-12-16 16:58 sendEmail_demo\src\com\cn\MailUtil2.java
文件 392 2017-12-16 16:39 sendEmail_demo\src\com\cn\Test.java
- 上一篇:推箱子java代码
- 下一篇:会议室管理 面向对象课设
评论
共有 条评论