javamail使用笔记

来源:互联网 发布:北京赛车辅助软件 编辑:程序博客网 时间:2024/04/30 21:57

本文描述了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().

原创粉丝点击