package a;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MyMailSender2
{
// private String hostName = "smtp.qq.com"; //可以根据邮箱地址截取例如 123@qq.com,12344@163.com
// private String userName = ""; //数据库获取
// private String userPwd = ""; //授权码????
// private String fromUserMail = "10.com"; //数据库获取
// private String toUserMail = "ren3.com"; //数据库获取
public void sendTestMail(String hostName,final String userName,final String userPwd,String fromUserMail,String toUserMail) throws AddressException, MessagingException
{
Properties mailProps = new Properties();
mailProps.setProperty("mail.smtp.host", hostName);
mailProps.setProperty("mail.smtp.starttls.enable", "true");
mailProps.setProperty("mail.smtp.auth", "true");
mailProps.setProperty("mail.smtp.quitwait", "false");

//如果不要对服务器的ssl证书进行受信任检查,测添加以下语句

mailProps.setProperty("mail.smtp.ssl.trust","*");

Session mailSession = Session.getDefaultInstance(mailProps,
new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName, userPwd);
}
});

Message mailMessage = new MimeMessage(mailSession);
mailMessage.setFrom(new InternetAddress(fromUserMail));
mailMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parseHeader(toUserMail, false));

mailMessage.setSubject("This is a test mail.");//发送标题
mailMessage.setText("This is a test mail for gmail");//发送内容
mailMessage.setSentDate(new Date());

Transport.send(mailMessage);
}

public static void main(String[] args)
{
try
{
new MyMailSender2().sendTestMail(null, null, null, null, null);
//update邮件发送状态
}
catch (AddressException e)
{
e.printStackTrace();
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package a;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message.RecipientType;
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 ServSendEmailBySmtp {

private static Properties prop;
// 发件人
private static InternetAddress sendMan = null;


public static void sendHtmlAndAttach(String senduser,String sendpassword,
String send_name, ArrayList<String> receiveuser,ArrayList<String> cc_user,
String hostAddress,String title,String content_info,ArrayList<String[]> fileinfo) throws Exception {
prop = new Properties();
prop.put("mail.transport.protocol", "smtp"); // 指定协议
prop.put("mail.smtp.host", hostAddress); // 主机
prop.put("mail.smtp.port", 25); // 端口
prop.put("mail.smtp.auth", "true"); // 用户密码认证
prop.put("mail.debug", "true"); // 调试模式
sendMan = new InternetAddress(senduser, send_name, "UTF-8");//发送的人员
Address[] to_all=new Address[receiveuser.size()]; //发送人
Address[] cc_all=new Address[cc_user.size()];//抄送人

if(to_all !=null && to_all.length>0){
for (int i = 0; i < receiveuser.size(); i++) {
to_all[i]=new InternetAddress(receiveuser.get(i));
}
}

if(cc_all !=null && cc_all.length>0){
for (int i = 0; i < cc_user.size(); i++) {
cc_all[i]=new InternetAddress(cc_user.get(i));
}
}

// 1. 创建邮件会话
Session session = Session.getDefaultInstance(prop);
// 2. 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 3. 设置参数:标题、发件人、收件人、发送时间、内容
message.setSubject(title);
// message.setSender(sendMan);
message.setFrom (sendMan);//设置发送人名称
//message.setRecipient(RecipientType.TO, new InternetAddress(receiveuser));//单个
message.setRecipients(RecipientType.TO, to_all);
message.setSentDate(new Date());
message.addRecipients(RecipientType.CC, cc_all); //抄送
// message.addRecipients(RecipientType.BCC, all);
/*
* 带附件(图片)邮件开发
*/
// 构建一个总的邮件块
MimeMultipart mixed = new MimeMultipart("mixed");
// ---> 总邮件快,设置到邮件对象中
message.setContent(mixed);
// 左侧: (文本+图片资源)
MimeBodyPart left = new MimeBodyPart();
// 右侧: 附件

// 设置到总邮件块
mixed.addBodyPart(left);

/******附件********/
if (fileinfo!=null && fileinfo.size()>0) {
for (int i = 0; i < fileinfo.size(); i++) {
MimeBodyPart right = new MimeBodyPart();
DataSource attr_ds = new FileDataSource(new File(fileinfo.get(i)[0]));
DataHandler attr_handler = new DataHandler(attr_ds);
right.setDataHandler(attr_handler);
right.setFileName(MimeUtility.encodeWord(fileinfo.get(i)[1]));
mixed.addBodyPart(right);
}
}

/***************设置邮件内容: 多功能用户邮件 (related)*******************/
// 4.1 构建一个多功能邮件块
MimeMultipart related = new MimeMultipart("related");
// ----> 设置到总邮件快的左侧中
left.setContent(related);

// 4.2 构建多功能邮件块内容 = 左侧文本 + 右侧图片资源
MimeBodyPart content = new MimeBodyPart();
/* MimeBodyPart resource = new MimeBodyPart();

// 设置具体内容: a.资源(图片)
String filePath = ServSendEmailBySmtp.class.getResource("8.jpg").getPath();
DataSource ds = new FileDataSource(new File(filePath));
DataHandler handler = new DataHandler(ds);
resource.setDataHandler(handler);
resource.setContentID("8.jpg"); // 设置资源名称,给外键引用
*/
// 设置具体内容: b.文本
content.setContent(content_info, "text/html;charset=UTF-8");

related.addBodyPart(content);
// related.addBodyPart(resource);

// 5. 发送
Transport trans = session.getTransport();
trans.connect(senduser, sendpassword);
trans.sendMessage(message, message.getAllRecipients());
trans.close();

}
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
ArrayList<String> arrayList1 = new ArrayList<String>();
arrayList1.add("renbomonkey@163.com");
ArrayList<String> arrayList2 = new ArrayList<String>();
new ServSendEmailBySmtp().sendHtmlAndAttach("1097204589", "mgjgimuvlpgygiji",
"1097204589@qq.com", arrayList1, arrayList2, "smtp.qq.com", "toncent:zhuti", "content:cneirong", null);
}
}