📄 sendmail.java
字号:
package cn.edu.scut.smimeapi;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.Multipart;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import sun.misc.BASE64Encoder;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
class SmtpAuth extends Authenticator
{
private String user, password;
/**
* to set the smtp auth's username and passwored
* @param getuser String
* @param getpassword String
*/
public void setUserinfo(String getuser, String getpassword)
{
user = getuser;
password = getpassword;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}
}
public class SendMail {
protected String host=""; //smtp host
protected String port="25"; //smtp host port DEfault port:25
protected String name=""; //smtp auth username
protected String password=""; //smtp auth password
protected static Session session=null;
//设置SMTP服务器、端口、验证用户名和密码
/**
* @param host String
* @param port String
* @param name String
* @param password String
*/
public void setAccount(String host,String port,String name,String password)
{ this.host=host;
this.port=port;
this.name=name;
this.password=password;
}
public void setAccount(String host,String name,String password)
{ this.host=host;
this.port="25";
this.name=name;
this.password=password;
}
//创建一个普通文本邮件,返回邮件实例对象MimeMessage
/**
* @param from String
* @param to String
* @param subject String
* @param content String
* @return body MimeMessage
*/
public MimeMessage CreateMail(String from,String to,String subject,String content)
throws Exception
{
Properties props = System.getProperties();
SmtpAuth sa = new SmtpAuth();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
sa.setUserinfo(name, password);
session = Session.getInstance(props, sa);
session.setDebug(true);
MimeMessage body = new MimeMessage(session);
body.setFrom(new InternetAddress(from));
body.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
body.setSubject(subject);
//body.setContent(content, "text/plain");
body.setText(content);
body.saveChanges();
//Transport.send(body);
return body;
}
//创建一个包含附件的邮件,返回邮件实例对象MimeMessage
/**
* @param from String
* @param to String
* @param subject String
* @param content String
* @param attachment String the attchment's filepath
* @return body MimeMessage
*/
public MimeMessage CreateMailWithAttchment(String from,String to,String subject,String content,String attachment)
throws Exception
{
if(attachment.equals(""))
return CreateMail(from,to,subject,content);
File file = new File(attachment);
if (!file.isFile())
{
System.err.println("无效的附件名!");
return CreateMail(from,to,subject,content);
}
Properties props = System.getProperties();
SmtpAuth sa = new SmtpAuth();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
sa.setUserinfo(name, password);
session = Session.getInstance(props, sa);
session.setDebug(true);
MimeMessage body = new MimeMessage(session);
body.setFrom(new InternetAddress(from));
body.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
body.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(content);
// create the second message part-+
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
BASE64Encoder enc = new BASE64Encoder();//该类位于jre/lib/rt.jar中
//fds为FileDataSource实例
mbp2.setFileName("=?GBK?B?"+enc.encode((fds.getName()).getBytes())+"?=");
//解决中文附件名乱码的问题
//mbp2.setFileName(fds.getName());
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// add the Multipart to the message
body.setContent(mp,mp.getContentType());
body.saveChanges();
//Transport.send(body);
return body;
}
public Session GetSession()
{
return this.session;
}
//发送邮件
public void Send (MimeMessage msg)throws Exception
{
try
{
Transport.send(msg);
System.out.println("邮件成功发送........");
}
catch (Exception ex)
{
throw new Exception(ex);
}
}
/*
public static void main(String args[])
{
try{
SendMail sm=new SendMail();
sm.setAccount("202.38.212.1","yhliu","yhliu");
String afile="E:\\附件中文问题.zip";
// sm.Send(sm.CreateMail("yhliu@saturn.tanglab.net","yhliu@saturn.tanglab.net","test send mail中文问题","hello,world!中文问题啊"));
sm.Send(sm.CreateMailWithAttchment("yhliu@saturn.tanglab.net","yhliu@saturn.tanglab.net","test send mail主题中文问题","hello,world!正文中文问题啊",afile));
}
catch (Exception ex)
{
ex.printStackTrace(System.err);
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -