SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");//指定SMTP server
email.addTo("[email protected]", "John Doe");//指定接收方地址與名字
email.setFrom("[email protected]", "Me");//指定發送方地址與名字
email.setSubject("Test message");//指定郵件標題
email.setMsg("This is a simple test of commons-email");//指定郵件內容
email.send();//發送
|
將
email.setMsg("This is a simple test of commons-email");
替換爲
email.setContent("This is a simple test of commons-email", "text/plain;charset=GBK");
|
或者如下方法:
工程目錄結構如下:
以下給出兩個簡單示例程序
// SendMail.java 使用SimpleEmail發郵件
package com.apache.commons.email.demo;
import org.apache.commons.mail.SimpleEmail;
public class SendMail
{
public static void main ( String[] arg ) throws Exception
{
// 使用SimpleEmail對於中文內容,可能會產生亂碼
SimpleEmail email = new SimpleEmail ( );
// SMTP服務器名
email.setHostName ( "smtp.163.com" );
// 登陸郵件服務器的用戶名和密碼
email.setAuthentication ( "peki", "123456" );
// 接收人
email.addTo ( "", "曦勤" );
// 發送人
email.setFrom ( "", "小陳" );
// 標題
email.setSubject ( "Test message" );
// 郵件內容
email.setMsg ( "This is a simple test of commons-email<br>我是小陳" );
// 發送
email.send ( );
System.out.println ( "Send email successful!" );
}
}
收信結果如下:
This is a simple test of commons-email<br>????
有亂碼產生,並且html內容沒有正常顯示
// MailSender.java 使用HtmlEmail發郵件
package com.apache.commons.email.demo;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class MailSender {
public static void main(String[] args) {
// 不要使用SimpleEmail,會出現亂碼問題
HtmlEmail email = new HtmlEmail();
try {
// 這裏是SMTP發送服務器的名字:,163的如下:
email.setHostName("smtp.163.com");
// 字符編碼集的設置
email.setCharset("gbk");
// 收件人的郵箱
email.addTo("");
// 發送人的郵箱
email.setFrom("", "小陳");
// 如果需要認證信息的話,設置認證:用戶名-密碼。分別爲發件人在郵件服務器上的註冊名稱和密碼
email.setAuthentication("peki", "123456");
email.setSubject("下午3:00會議室討論,請準時參加");
// 要發送的信息,由於使用了HtmlEmail,可以在郵件內容中使用HTML標籤
email.setMsg("下午3:00會議室討論,請準時參加<BR>呵呵~!");
// 發送
email.send();
System.out.println ( "郵件發送成功!" );
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println ( "郵件發送失敗!" );
}
}
}
收信結果如下:
下午3:00會議室討論,請準時參加
// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg");//指定附件在本地的路徑
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");//附件描述
attachment.setName("John");//附件名稱
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
email.addTo("[email protected]", "John Doe");
email.setFrom("[email protected]", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
email.attach(attachment);
// send the email
email.send();
|
attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
|
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");//附件描述
attachment.setName("需傳送的附件");//附件名稱
attachment.setName(MimeUtility.encodeText("需傳送的附件.txt"));
|
public SendMail(String hostSMTP, String toMailAddress, String toMailUser,
String fromMailAddress, String fromMailUser, String mailSubject,
String mailMessage) {
|