JAVA傳送Email郵件

2021-08-31 12:57:12 字數 3043 閱讀 9271

使用apache的commons email傳送郵件

首先在pom中新增依賴:

org.apache.commons

commons-email

1.2

1、傳送簡單郵件:

email email = new ******email();

email.sethostname("smtp.gmail.com");

email.setsmtpport(587);

email.setauthenticator(new defaultauthenticator("username", "password"));

email.settls(true);

email.setfrom("[email protected]");

email.setsubject("testmail");

email.setmsg("this is a test mail ... :-)");

email.addto("[email protected]");

email.send();

2、傳送帶附件的郵件:

3、傳送html格式的郵件:

import org.apache.commons.mail.htmlemail;

...// create the email message

htmlemail email = new htmlemail();

email.sethostname("mail.myserver.com");

email.addto("[email protected]", "john doe");

email.setfrom("[email protected]", "me");

email.setsubject("test email with inline image");

// embed the image and get the content id

url url = new url("");

string cid = email.embed(url, "apache logo");

// set the html message

email.sethtmlmsg("the apache logo -

// set the alternative message

email.settextmsg("your email client does not support html messages");

// send the email

email.send();

4、傳送帶的html郵件:

import org.apache.commons.mail.htmlemail;

...// load your html email template

string htmlemailtemplate = ....

// create the email message

htmlemail email = new imagehtmlemail();

email.sethostname("mail.myserver.com");

email.addto("[email protected]", "john doe");

email.setfrom("[email protected]", "me");

email.setsubject("test email with inline image");

// embed the image and get the content id

url url = new url("");

string cid = email.embed(url, "apache logo");

// set the html message

email.sethtmlmsg(htmlemailtemplate, new file("").touri().tourl(), false);

// set the alternative message

email.settextmsg("your email client does not support html messages");

// send the email

email.send();

[quote]commons email傳送郵件使用者指南:[url]

linux shell 傳送email 郵件

方法一 簡單郵件傳送 echo hello world mail s subject t yanggang ithomer.com,yanggang 2050 163.com a from 463103470 qq.com 效果截圖 方法二 文字格式傳送郵件 python view plain co...

php傳送郵件email

header content type text html charset utf 8 include class.phpmailer.php include class.smtp.php mail new phpmailer mail issmtp 設定使用smtp伺服器傳送 mail host ...

linux shell 傳送email 郵件

方法一 簡單郵件傳送 echo hello world mail s subject t yanggang ithomer.com,yanggang 2050 163.com a from 463103470 qq.com 效果截圖 方法二 文字格式傳送郵件 bin shfrom name from...