java发送邮件代码 java给邮箱发邮件

java实现发送邮件功能

要实现邮件发送功能需要导入包:mail.jar

创新互联是一家集网站建设,邯山企业网站建设,邯山品牌网站建设,网站定制,邯山网站建设报价,网络营销,网络优化,邯山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

/*

* Generated by MyEclipse Struts

* Template path: templates/java/JavaClass.vtl

*/

package org.demo.action;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.demo.form.DemoForm;

public class DemoAction extends Action {

private static final String CONTENT_TYPE = "test/html;charset=GB2312";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

DemoForm demoForm = (DemoForm) form;

System.out.println("标题是" + demoForm.getBiaoti());

System.out.println("内容是" + demoForm.getNeirong());

try {

response.setContentType(CONTENT_TYPE);

String smtphost = "smtp.nj.headware.cn"; // 发送邮件服务器

String user = "q0000015369"; // 邮件服务器登录用户名

String password = "Queshuwen26"; // 邮件服务器登录密码

String from = "q0000015369@nj.headware.cn"; //

String to = "c0000016205@nj.headware.cn"; // 收件人邮件地址

String subject = demoForm.getBiaoti(); // 邮件标题

String body = demoForm.getNeirong(); // 邮件内容

Properties props = new Properties();

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

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

Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);

message.setFrom(fromAddress);

InternetAddress toAddress = new InternetAddress(to);

message.addRecipient(Message.RecipientType.TO, toAddress);

message.setSubject(subject);

message.setText(body);

Transport transport = ssn.getTransport("smtp");

transport.connect(smtphost, user, password);

transport.sendMessage(message, message

.getRecipients(Message.RecipientType.TO));

// transport.send(message);

transport.close();

return mapping.findForward("succ");

} catch (Exception e) {

e.printStackTrace();

return mapping.findForward("fail");

}

}

}

用java写一个邮件发送代码

public boolean mainto()

{

boolean flag = true;

//建立邮件会话

Properties pro = new Properties();

pro.put("mail.smtp.host","smtp.qq.com");//存储发送邮件的服务器

pro.put("mail.smtp.auth","true"); //通过服务器验证

Session s =Session.getInstance(pro); //根据属性新建一个邮件会话

//s.setDebug(true);

//由邮件会话新建一个消息对象

MimeMessage message = new MimeMessage(s);

//设置邮件

InternetAddress fromAddr = null;

InternetAddress toAddr = null;

try

{

fromAddr = new InternetAddress(451144426+"@qq.com"); //邮件发送地址

message.setFrom(fromAddr); //设置发送地址

toAddr = new InternetAddress("12345367@qq.com"); //邮件接收地址

message.setRecipient(Message.RecipientType.TO, toAddr); //设置接收地址

message.setSubject(title); //设置邮件标题

message.setText(content); //设置邮件正文

message.setSentDate(new Date()); //设置邮件日期

message.saveChanges(); //保存邮件更改信息

Transport transport = s.getTransport("smtp");

transport.connect("smtp.qq.com", "451144426", "密码"); //服务器地址,邮箱账号,邮箱密码

transport.sendMessage(message, message.getAllRecipients()); //发送邮件

transport.close();//关闭

}

catch (Exception e)

{

e.printStackTrace();

flag = false;//发送失败

}

return flag;

}

这是一个javaMail的邮件发送代码,需要一个mail.jar

怎么样使用JavaMail发送和接收邮件

public class MailTest {

//发送的邮箱 内部代码只适用qq邮箱

private static final String USER = "xxxxx@qq.com";

//授权密码 通过QQ邮箱设置-账户-POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务-开启POP3/SMTP服务获取

private static final String PWD = "xxxxx";

private String[] to;

private String[] cc;//抄送

private String[] bcc;//密送

private String[] fileList;//附件

private String subject;//主题

private String content;//内容,可以用html语言写

public void sendMessage() throws Exception {

// 配置发送邮件的环境属性

final Properties props = new Properties();

//下面两段代码是设置ssl和端口,不设置发送不出去。

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

//props.setProperty("mail.smtp.port", "465");

props.setProperty("mail.smtp.socketFactory.port", "465");

// 表示SMTP发送邮件,需要进行身份验证

props.setProperty("mail.transport.protocol", "smtp");// 设置传输协议

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

props.put("mail.smtp.host", "smtp.qq.com");//QQ邮箱的服务器 如果是企业邮箱或者其他邮箱得更换该服务器地址

// 发件人的账号

props.put("mail.user", USER);

// 访问SMTP服务时需要提供的密码

props.put("mail.password", PWD);

// 构建授权信息,用于进行SMTP进行身份验证

Authenticator authenticator = new Authenticator() {

@Override

protected PasswordAuthentication getPasswordAuthentication() {

// 用户名、密码

String userName = props.getProperty("mail.user");

String password = props.getProperty("mail.password");

return new PasswordAuthentication(userName, password);

}

};

// 使用环境属性和授权信息,创建邮件会话

Session mailSession = Session.getInstance(props, authenticator);

// 创建邮件消息

MimeMessage message = new MimeMessage(mailSession);

BodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();

// 设置发件人

InternetAddress form = new InternetAddress(

props.getProperty("mail.user"));

message.setFrom(form);

//发送

if (to != null) {

String toList = getMailList(to);

InternetAddress[] iaToList = new InternetAddress().parse(toList);

message.setRecipients(RecipientType.TO, iaToList); // 收件人

}

//抄送

if (cc != null) {

String toListcc = getMailList(cc);

InternetAddress[] iaToListcc = new InternetAddress().parse(toListcc);

message.setRecipients(RecipientType.CC, iaToListcc); // 抄送人

}

//密送

if (bcc != null) {

String toListbcc = getMailList(bcc);

InternetAddress[] iaToListbcc = new InternetAddress().parse(toListbcc);

message.setRecipients(RecipientType.BCC, iaToListbcc); // 密送人

}

message.setSentDate(new Date()); // 发送日期 该日期可以随意写,你可以写上昨天的日期(效果很特别,亲测,有兴趣可以试试),或者抽象出来形成一个参数。

message.setSubject(subject); // 主题

message.setText(content); // 内容

//显示以html格式的文本内容

messageBodyPart.setContent(content,"text/html;charset=utf-8");

multipart.addBodyPart(messageBodyPart);

//保存多个附件

if(fileList!=null){

addTach(fileList, multipart);

}

message.setContent(multipart);

// 发送邮件

Transport.send(message);

}

public void setTo(String[] to) {

this.to = to;

}

public void setCc(String[] cc) {

this.cc = cc;

}

public void setBcc(String[] bcc) {

this.bcc = bcc;

}

public void setSubject(String subject) {

this.subject = subject;

}

public void setContent(String content) {

this.content = content;

}

public void setFileList(String[] fileList) {

this.fileList = fileList;

}

private String getMailList(String[] mailArray) {

StringBuffer toList = new StringBuffer();

int length = mailArray.length;

if (mailArray != null length 2) {

toList.append(mailArray[0]);

} else {

for (int i = 0; i length; i++) {

toList.append(mailArray[i]);

if (i != (length - 1)) {

toList.append(",");

}

}

}

return toList.toString();

}

//添加多个附件

public void addTach(String fileList[], Multipart multipart) throws Exception {

for (int index = 0; index fileList.length; index++) {

MimeBodyPart mailArchieve = new MimeBodyPart();

FileDataSource fds = new FileDataSource(fileList[index]);

mailArchieve.setDataHandler(new DataHandler(fds));

mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),"UTF-8","B"));

multipart.addBodyPart(mailArchieve);

}

}

//以下是演示demo

public static void main(String args[]) {

MailTest mail = new MailTest();

mail.setSubject("java邮件");

mail.setContent("你好 这是第一个java 程序发送邮件");

//收件人 可以发给其他邮箱(163等) 下同

mail.setTo(new String[] {"xxxxx@qq.com"});

//抄送

// mail.setCc(new String[] {"xxxxx@qq.com","xxxxx@qq.com"});

//密送

//mail.setBcc(new String[] {"xxxxx@qq.com","xxxxx@qq.com"});

//发送附件列表 可以写绝对路径 也可以写相对路径(起点是项目根目录)

// mail.setFileList(new String[] {"D:\\aa.txt"});

//发送邮件

try {

mail.sendMessage();

System.out.println("发送邮件成功!");

} catch (Exception e) {

System.out.println("发送邮件失败!");

e.printStackTrace();

}

}

}


当前名称:java发送邮件代码 java给邮箱发邮件
地址分享:http://hbruida.cn/article/dosepdh.html