javamail使用笔记
2006-02-02 13:26
281 查看
本文描述了3个常见的javamail问题,至于其它问题可以看javamail的例子
1:怎样发送认证邮件
不想以前那样,很多smtp服务器已经不能随便让你通过它发信,你必须提供用户名和密码才能发信,下面的示例演示了这样的一种情况。
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailExample {
public static void main (String args[]) throws Exception ,MessagingException{
String host = "smtp.263.net";
String from = "zong_feng@263.net";
String to = "zfvc@163.com";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
message.saveChanges();
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, "zong_feng","111111");
transport.sendMessage(message, message.getAllRecipients());
}
}
2:发送附件
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class sendfile {
public static void main(String[] args) {
String to = "zfvc@163.com";
String from = "zong_feng@263.net";
String host = "smtp.263.net";
String filename = args[0];
//boolean debug = Boolean.valueOf(args[4]).booleanValue();
String msgText1 = "宗锋.n";
String subject = "Sending a file";
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
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
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport transport = session.getTransport("smtp");
transport.connect(host, "zong_feng","dsf");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
3:怎样是IMAP和POP3一起工作
Use different session objects (don't use the default). Get the session with Session.getInstance() instead of getDefaultInstance().
1:怎样发送认证邮件
不想以前那样,很多smtp服务器已经不能随便让你通过它发信,你必须提供用户名和密码才能发信,下面的示例演示了这样的一种情况。
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailExample {
public static void main (String args[]) throws Exception ,MessagingException{
String host = "smtp.263.net";
String from = "zong_feng@263.net";
String to = "zfvc@163.com";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
message.saveChanges();
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, "zong_feng","111111");
transport.sendMessage(message, message.getAllRecipients());
}
}
2:发送附件
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class sendfile {
public static void main(String[] args) {
String to = "zfvc@163.com";
String from = "zong_feng@263.net";
String host = "smtp.263.net";
String filename = args[0];
//boolean debug = Boolean.valueOf(args[4]).booleanValue();
String msgText1 = "宗锋.n";
String subject = "Sending a file";
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
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
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport transport = session.getTransport("smtp");
transport.connect(host, "zong_feng","dsf");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
3:怎样是IMAP和POP3一起工作
Use different session objects (don't use the default). Get the session with Session.getInstance() instead of getDefaultInstance().
相关文章推荐
- JavaMail学习笔记-3 (包括Oreilly欧莱理上传组件的使用)
- 黑马程序员---OC学习笔记之NSString的其他使用和NSMutableString的用法
- PDO使用笔记
- spring boot Websocket(使用笔记)
- IntelliJ IDEA 学习笔记 - Live Templates的使用
- curl使用笔记
- 【笔记】发送邮件工具类的使用
- (转载)JavaWeb学习总结(五十三)——Web应用中使用JavaMail发送邮件
- PHP-Java-Bridge使用笔记
- [Effective JavaScript 笔记]第43条:使用Object的直接实例构造轻量级的字典
- UCOS+UCTCP/IP 协议初步使用笔记
- springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置
- Xcode 使用笔记
- php学习笔记(三十六)smarty中使用自定函数和代码块(smarty2和smarty3的不同)
- JDBC 学习笔记(十)—— 使用 JDBC 搭建一个简易的 ORM 框架
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(五)i2c读取测试
- 算法笔记四:使用分治策略求最大子数组算法
- C++学习笔记四——何时使用引用参数
- VIM使用学习笔记 : 按键说明
- javascript学习笔记(九):DOM操作HTML的各种方法使用