欢迎光临
我们一直在努力

JavaMail操作的总结(1)-JSP教程,邮件相关

建站超值云服务器,限时71元/月

在整理网友的文章的时候,发现一个javamail的总结,特此谢谢the_east_key,并且公布给大家,希望对大家有做帮助,全文如下:

本文章对:

发送普通邮件,接受普通邮件

发送带有附件的邮件,接收带有附件的邮件

发送html形式的邮件,接受html形式的邮件

[b[发送带有图片的邮件[/b]等做了一个总结。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

import javax.mail.*;

import java.util.*;

import javax.mail.internet.*;

import java.io.*;

import javax.activation.*;

public string host="smtp.163.com";

public string username="abcdefg";

public string password="abcdefg";

public string mail_head_name="this is head of this mail";

public string mail_head_value="this is head of this mail";

public string mail_to="xyz@163.com";

public string mail_from="abcdefg@163.com";

public string mail_subject="this is the subject of this test mail";

public string mail_body="this is the mail_body of this test mail";

//此段代码用来发送普通电子邮件

void jbutton1_actionperformed(actionevent e) {

try

{

properties props = new properties();//获取系统环境

authenticator auth = new email_autherticator();//进行邮件服务器用户认证

props.put("mail.smtp.host",host);

props.put("mail.smtp.auth","true");

session session = session.getdefaultinstance(props,auth);

//设置session,和邮件服务器进行通讯。

mimemessage message = new mimemessage(session);

message.setcontent("hello","text/plain");//设置邮件格式

message.setsubject(mail_subject);//设置邮件主题

message.settext(mail_body);//设置邮件正文

message.setheader(mail_head_name,mail_head_value);//设置邮件标题

message.setsentdate(new date());//设置邮件发送日期

address address = new internetaddress(mail_from,"sunxiaoming");

message.setfrom(address); //设置邮件发送者的地址

//如果要对邮件发送者进行多个参数的设置,可以用以下语句

// address address[] = {new internetaddress("sunxm@oaklet.co.jp","sunxmatoaklet"),new internetaddress("firstsxm@hotmail.com","sunxmathotmail")};

// message.addfrom(address);

address toaddress = new internetaddress(mail_to);//设置邮件接收方的地址

message.addrecipient(message.recipienttype.to,toaddress);

// address ccaddress = new internetaddress("firstsxm@hotmail.com");//设置邮件抄送者的地址

// message.addrecipient(message.recipienttype.cc,ccaddress);

transport.send(message);//发送邮件

/* // to get a specific instance from the session for your protocol.pass along the username and password

// (blank if unnecessary).send the message,and close the connection;

message.savechanges();

transport transport = session.gettransport("smtp");

transport.connect(host,username,password);

transport.sendmessage(message,message.getallrecipients());

transport.close();

*/

system.out.println("send ok!");

}

catch(exception ex)

{

system.out.println("faild"+ex);

}

}

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » JavaMail操作的总结(1)-JSP教程,邮件相关
分享到: 更多 (0)