本文描述了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 = "[email protected]";
String to = "[email protected]";
// 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 = "[email protected]";
String from = "[email protected]";
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使用筆記
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.