java html邮件_javamail发送html邮件

发送文本型文件

SMTP服务器:

登录账号:

登录密码:

发件人邮箱:

收件人邮箱:

邮件主题:

邮件内容:

2.SendHtmlMail.java

package com.lhb.mail;

import java.util.Date;

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;

public class SendHtmlMail {

String SMTPHost="";

String user="";

String password="";

String from="";

String to="";

String subject="";

String content="";

public SendHtmlMail(){

}

public String getSMTPHost() {

return SMTPHost;

}

public void setSMTPHost(String host) {

SMTPHost = host;

}

public String getUser() {

return user;

}

public void setUser(String user) {

this.user = user;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getFrom() {

return from;

}

public void setFrom(String from) {

this.from = from;

}

public String getTo() {

return to;

}

public void setTo(String to) {

this.to = to;

}

public String getSubject() {

return subject;

}

public void setSubject(String subject) {

try {

subject=new String(subject.getBytes("ISO8859-1"),"utf-8");

} catch (Exception e) {

e.printStackTrace();

}

this.subject = subject;

}

public String getContent() {

return content;

}

public void setContent(String content) {

try {

content=new String(content.getBytes("ISO8859-1"),"utf-8");

} catch (Exception e) {

e.printStackTrace();

}

this.content = content;

}

public boolean send(){

//创建一个属性对象

Properties props=new Properties();

//指定smtp服务器

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

//指定是否需要smtp验证

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

try {

//创建一个授权验证对象

SmtpAuth auth=new SmtpAuth();

auth.setAccount(user, password);

//创建一个session对象

Session mailSession=Session.getDefaultInstance(props);

mailSession.setDebug(true);

//创建一个Message对象

Message message=new MimeMessage(mailSession);

//指定发件人邮箱

message.setFrom(new InternetAddress(from));

//指定收件人邮箱

message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

//指定邮箱主题

message.setSubject(subject);

//指定邮箱内容及ContentType和编码方式

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

//指定邮件发送日期

message.setSentDate(new Date());

//指定邮件优先级 1:紧急 3:普通 5:缓慢

message.setHeader("X-Priority", "1");

message.saveChanges();

//创建一个Transport对象

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

//连接SMTP服务器

transport.connect(SMTPHost,user, password);

//发送邮件

transport.sendMessage(message, message.getAllRecipients());

transport.close();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

}

3.SmtpAuth.java

package com.lhb.mail;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class SmtpAuth extends Authenticator {

String user,password;

//设置账号信息

void setAccount(String user,String password){

this.user=user;

this.password=password;

}

//取得PsswordAuthentication对象

protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(user,password);

}

}

4.sendMail2.jsp

boolean status=mySend.send();

if(status){

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

}

else

{

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

}

%>

posted on 2008-05-23 10:21 林的 阅读(4385) 评论(3)  编辑  收藏 所属分类: util

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值