怎么实现 ssh发送邮件 请高手帮忙解决一下
解决方案 »
- 一段SQL变成HQL怎么写
- java中如果解析rquest中json数据
- ajax的onreadyStateChange不触发
- 求仪表盘....
- 我还差多少路要走?一个转型的人的提问!
- Resin中怎样发布Web Service问题
- WebServices教材指点
- 急救!!!!!!!!!多谢大家了!!!!!!!!!!!!!!!!!在线等
- hibernate3 更新数据问题,大家有碰到过的没?
- 目前最常用的java软件及用法
- JBOSS JNDI报NameNotFoundException: MYSQL_JTA not bound 错误
- 为什么我的J2EE WEB程序一开始运行没错,再一次运行就整个程序全是错了呢?
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
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;public class MailUtil { private String host; private String from; private String to; private String subject; private String content; private boolean authentication; private String username; private String password;
private boolean isAttachement = false; //是否有附件
//private String[] fileNames; //附件名称
public MailUtil() {};
public void sendMail(String host, String from, String to, String subject,
String content, boolean authentication, String username,
String password,boolean isAttachement,List fileNames) throws MessagingException {
// Get system properties
//Properties props = System.getProperties();
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", 25);
if (!authentication) {
props.put("mail.smtp.auth", "false");
} else {
props.put("mail.smtp.auth", "true");
}
// Get session
//Session session = Session.getDefaultInstance(props, null);
Session session = Session.getInstance(props);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
Multipart multipart = new MimeMultipart();
//邮件正文
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(content);
multipart.addBodyPart(mbp1);
//附件发送
if(isAttachement){
this.addAttachement(fileNames, message, multipart);
} //邮件内容
message.setContent(multipart);
// Send message
if (authentication) {
Transport smtp = null;
try {
smtp = session.getTransport("smtp");
smtp.connect(host, username, password);
smtp.sendMessage(message, message.getAllRecipients());
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
smtp.close();
}
} else {
Transport.send(message);
}
}
//添加附件
public void addAttachement(List fileNames,MimeMessage message,Multipart multipart){
for(int i = 0;i < fileNames.size();i++){
try {
String fileAttachment = fileNames.get(i).toString();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
//设置文件名称
String newFileName = fileAttachment.substring(fileAttachment.lastIndexOf("/")+1);
//解决文件名乱码
messageBodyPart.setFileName(MimeUtility.encodeText(newFileName));
multipart.addBodyPart(messageBodyPart); // Put parts in message
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 带附件的发送方法
* @param to
* @param subject
* @param content
* @param fileNames
*/
public String send(String to,String subject,String content,List fileNames) {
host = "mail.mediaat.net";
//from = "[email protected]";
//to = "[email protected]";
//subject = "just for test";
//content = "http://comment2.news.sohu.com";
from = "[email protected]";
this.to = to;
this.content = content;
this.subject = subject;
authentication = true;
//username = "[email protected]";
//password = "8810701";
username = "[email protected]";
password = "123456";
//String[] fieleNames = {"E:/图片素材/1.jpg","E:/图片素材/3.docx"};
try {
sendMail(host, from, this.to, this.subject,this.content, authentication, username,
password,true,fileNames);
System.out.println("发送成功");
return "发送成功";
} catch (Exception e) {
System.out.println("mail");
e.printStackTrace();
return "发送失败";
}
} /**
* 不带附件的发送发
* @param to
* @param subject
* @param content
*/
public String send(String to,String subject,String content) {
host = "mail.mediaat.net";
//from = "[email protected]";
//to = "[email protected]";
//subject = "just for test";
//content = "http://comment2.news.sohu.com";
from = "[email protected]";
this.to = to;
this.content = content;
this.subject = subject;
authentication = true;
//username = "[email protected]";
//password = "8810701";
username = "[email protected]";
password = "123456";
//String[] fieleNames = {"E:/图片素材/1.jpg","E:/图片素材/3.docx"};
try {
sendMail(host, from, this.to, this.subject,this.content, authentication, from,
password,false,new ArrayList());
System.out.println("发送成功");
return "发送成功";
} catch (MessagingException e) {
System.out.println("mail");
e.printStackTrace();
return "发送失败";
}
} public static void main(String args[]) throws MessagingException {
List fileNames = new ArrayList();
fileNames.add("d:/图片素材/1.jpg");
fileNames.add("E:/图片素材/我的文档.docx");
MailUtil jm = new MailUtil();
jm.send("[email protected]","111","222",fileNames);
}}