后台自动发邮件--java代码
2010-02-23 16:18
357 查看
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;
public class SendMail {
/**
* @param args
*/
public static void main(String[] args) {
Properties props = System.getProperties();
// 设置smtp服务器
props.setProperty("mail.smtp.host", "smtp.126.com");
// 现在的大部分smpt都需要验证了
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props);
// 为了查看运行时的信息
s.setDebug(true);
// 由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);
try {
// 发件人
InternetAddress from = new InternetAddress("fasongren@126.com");
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress("jieshouren@163.com");
message.setRecipient(Message.RecipientType.TO, to);
// 邮件标题
message.setSubject("test");
String content = "测试内容";
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
/****下面代码是发送附件******
String fileName = "d://hello.txt";
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);*/
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect("smtp.126.com", "username", "password");
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
/**
* @param args
*/
public static void main(String[] args) {
Properties props = System.getProperties();
// 设置smtp服务器
props.setProperty("mail.smtp.host", "smtp.126.com");
// 现在的大部分smpt都需要验证了
props.put("mail.smtp.auth", "true");
Session s = Session.getInstance(props);
// 为了查看运行时的信息
s.setDebug(true);
// 由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);
try {
// 发件人
InternetAddress from = new InternetAddress("fasongren@126.com");
message.setFrom(from);
// 收件人
InternetAddress to = new InternetAddress("jieshouren@163.com");
message.setRecipient(Message.RecipientType.TO, to);
// 邮件标题
message.setSubject("test");
String content = "测试内容";
// 邮件内容,也可以使纯文本"text/plain"
message.setContent(content, "text/html;charset=GBK");
/****下面代码是发送附件******
String fileName = "d://hello.txt";
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);*/
message.saveChanges();
Transport transport = s.getTransport("smtp");
// smtp验证,就是你用来发邮件的邮箱用户名密码
transport.connect("smtp.126.com", "username", "password");
// 发送
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- 在java后台用javacript写一段小代码实现html页面颜色的自动改变
- 配置axis2环境变量,wsdl2ava命令自动生成JAVA代码,学会查看wsdl文档
- java Linfaner批量添加课表后台服务代码
- android自动适应屏幕三、在java代码中设置宽高度
- ascc+asp自动注射代码,可以猜后台
- 后台自动登录网页vb脚本实例代码
- JavaWeb项目启动时,自动执行代码…
- Java后台代码调用Spring的@Service Bean的方式
- java后台实现支付宝对账功能的示例代码
- Java进阶之 如何自动生成代码
- linux下redis后台运行连接Java代码
- 通过.NET实现后台自动发送Email功能的代码示例
- JAVA中Mybatis连接oracle数据库自动生成代码工具
- [转]java版自动生成xml代码
- Web Service 简单实例(java 版本) ,IDE自动生成客户端代码方法
- 代码自动生成工具_java版
- SpringBoot系列: 使用MyBatis maven插件自动生成java代码
- 文件上传——java后台代码
- java后台代码打开一个链接,并获取json数据
- eclipse怎样对java代码自动排版,快速格式化,快速使代码对齐?