Javamail,编写简单的程序发送邮件
来源:互联网 发布:为人父母方知父母恩 编辑:程序博客网 时间:2024/06/15 11:20
代码:
package com.dai.mail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
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;
public class HtmlMessageSender {
String protocol = "smtp";
String from = "15829004147@sina.cn";
String to = "243047007@qq.com";
String subject = "test";
String body = "<a href=http://www.renren.com> " +
"welcome to our website!</a>" +
"this is a test mail from sina!";
/**
* create session and the session has been setted particular properties
* @return
*/
public Session createSession() {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", protocol);
/*必须将mail.smtp.auth属性设置为true,SMTPTransport对象才会向SMTP服务器提交认证
* 信息,这个信息可以从JavaMail的javadocs文档中的com.sun.mail.smtp包的帮助文档中
* 看到*/
props.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
return session;
}
/**
* create MimeMessage,used the MultiBodyPart and MimeBodyPart
* @param session
* @return
* @throws Exception
*/
public MimeMessage createMessage(Session session) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
MimeMultipart multiPart = new MimeMultipart("related");
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset = GBK");
multiPart.addBodyPart(htmlBodyPart);
MimeBodyPart gifBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("d://zzz//love.jpg");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("love_jpg");
multiPart.addBodyPart(gifBodyPart);
message.setContent(multiPart);
message.saveChanges();
return message;
}
public static void main(String[] args) throws Exception {
String server = "smtp.sina.com.cn";
String user = "15829004147@sina.cn";
String pass = "XXX";//密码
HtmlMessageSender sender = new HtmlMessageSender();
Session session = sender.createSession();
MimeMessage message = sender.createMessage(session);
//获取Transport对象,并连接邮件服务器发送邮件
Transport transport = session.getTransport();
transport.connect(server, user, pass);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
- Javamail,编写简单的程序发送邮件
- 【JavaMail开发总结】编写简单的发送邮件程序
- javaMail编写发送邮件的程序,以及遇到的问题
- JSP与JavaMail(6)---编写灵活的邮件发送程序
- JSP与JavaMail(6)---编写灵活的邮件发送程序
- 简单的JavaMail邮件发送
- javamail发送简单的邮件
- javamail简单的发送邮件
- javamail简单的邮件发送
- 【Java邮件开发】5.编写一个JavaMail发送邮件的程序
- JavaMail发送简单邮件
- JavaMail 发送简单邮件
- JavaMail 简单发送邮件
- javaMail简单发送邮件
- JavaMail发送邮件程序
- javamail发送邮件的简单实例
- 最简单的JavaMail发送邮件
- javamail发送邮件的简单实例
- 转载dawugui MS SQL
- JNDI
- Simpler Multithreading in C++0x
- 关于QlistWidget的currentRowChanged延时响应
- Pyinstaller是个好东西
- Javamail,编写简单的程序发送邮件
- ubuntu C 编程
- Cannot find a solution satisfying the following requirements org.eclipse.pde.core
- 基于Apache的HTTPClient的网页获取方法
- .net 2010视频教学网
- ubuntu10.04LTS 64bit 安装 kscope
- Bjarne Stroustrup Expounds on Concepts and the Future of C++
- java 处理大数
- 在RHEL 5.3(Tikanga)搭建Git