java发邮件需要什么意思_java 发送邮件需要开启什么服务

java 发送邮件需要开启什么服务

关注:149  答案:2  mip版

解决时间 2021-02-04 10:01

e6cb1a03ad541b3098697807b7bf1798.png

提问者美人如画皮

2021-02-03 14:35

java 发送邮件需要开启什么服务

最佳答案

e6cb1a03ad541b3098697807b7bf1798.png

二级知识专家臸釪樶初

2021-02-03 14:49

需要开启SMTP协议

全部回答

e6cb1a03ad541b3098697807b7bf1798.png

1楼为你卑微了我自己

2021-02-03 16:12

使用javamail发送邮件需要用到mail.jar和activtion.jar两个包。

该类实现了较完整的邮件发送功能,包括以html格式发送,添加附件和抄送人。下面是具体的代码:

package cn.cgw.util.mail;

import java.util.properties;

import javax.activation.datahandler;

import javax.activation.filedatasource;

import javax.mail.address;

import javax.mail.bodypart;

import javax.mail.message;

import javax.mail.multipart;

import javax.mail.session;

import javax.mail.transport;

import javax.mail.internet.internetaddress;

import javax.mail.internet.mimebodypart;

import javax.mail.internet.mimemessage;

import javax.mail.internet.mimemultipart;

public class mail {

private mimemessage mimemsg; //mime邮件对象

private session session; //邮件会话对象

private properties props; //系统属性

private boolean needauth = false; //smtp是否需要认证

//smtp认证用户名和密码

private string username;

private string password;

private multipart mp; //multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成mimemessage对象

public mail(string smtp){

setsmtphost(smtp);

createmimemessage();

}

public void setsmtphost(string hostname) {

system.out.println("设置系统属性:mail.smtp.host = "+hostname);

if(props == null)

props = system.getproperties(); //获得系统属性对象

props.put("mail.smtp.host",hostname); //设置smtp主机

}

public boolean createmimemessage()

{

try {

system.out.println("准备获取邮件会话对象!");

session = session.getdefaultinstance(props,null); //获得邮件会话对象

}

catch(exception e){

system.err.println("获取邮件会话对象时发生错误!"+e);

return false;

}

system.out.println("准备创建mime邮件对象!");

try {

mimemsg = new mimemessage(session); //创建mime邮件对象

mp = new mimemultipart();

return true;

} catch(exception e){

system.err.println("创建mime邮件对象失败!"+e);

return false;

}

}

public void setneedauth(boolean need) {

system.out.println("设置smtp身份认证:mail.smtp.auth = "+need);

if(props == null) props = system.getproperties();

if(need){

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

}else{

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

}

}

public void setnamepass(string name,string pass) {

username = name;

password = pass;

}

public boolean setsubject(string mailsubject) {

system.out.println("设置邮件主题!");

try{

mimemsg.setsubject(mailsubject);

return true;

}

catch(exception e) {

system.err.println("设置邮件主题发生错误!");

return false;

}

}

public boolean setbody(string mailbody) {

try{

bodypart bp = new mimebodypart();

bp.setcontent(""+mailbody,"text/html;charset=gbk");

mp.addbodypart(bp);

return true;

} catch(exception e){

system.err.println("设置邮件正文时发生错误!"+e);

return false;

}

}

public boolean addfileaffix(string filename) {

system.out.println("增加邮件附件:"+filename);

try{

bodypart bp = new mimebodypart();

filedatasource fileds = new filedatasource(filename);

bp.setdatahandler(new datahandler(fileds));

bp.setfilename(fileds.getname());

mp.addbodypart(bp);

return true;

} catch(exception e){

system.err.println("增加邮件附件:"+filename+"发生错误!"+e);

return false;

}

}

public boolean setfrom(string from) {

system.out.println("设置发信人!");

try{

mimemsg.setfrom(new internetaddress(from)); //设置发信人

return true;

} catch(exception e) {

return false;

}

}

public boolean setto(string to){

if(to == null)return false;

try{

mimemsg.setrecipients(message.recipienttype.to,internetaddress.parse(to));

return true;

} catch(exception e) {

return false;

}

}

public boolean setcopyto(string copyto)

{

if(copyto == null)return false;

try{

mimemsg.setrecipients(message.recipienttype.cc,(address[])internetaddress.parse(copyto));

return true;

}

catch(exception e)

{ return false; }

}

public boolean sendout()

{

try{

mimemsg.setcontent(mp);

mimemsg.savechanges();

system.out.println("正在发送邮件....");

session mailsession = session.getinstance(props,null);

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

transport.connect((string)props.get("mail.smtp.host"),username,password);

transport.sendmessage(mimemsg,mimemsg.getrecipients(message.recipienttype.to));

transport.sendmessage(mimemsg,mimemsg.getrecipients(message.recipienttype.cc));

//transport.send(mimemsg);

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

transport.close();

return true;

} catch(exception e) {

system.err.println("邮件发送失败!"+e);

return false;

}

}

public static boolean send(string smtp,string from,string to,string subject,string content,string username,string password) {

mail themail = new mail(smtp);

themail.setneedauth(true); //需要验证

if(!themail.setsubject(subject)) return false;

if(!themail.setbody(content)) return false;

if(!themail.setto(to)) return false;

if(!themail.setfrom(from)) return false;

themail.setnamepass(username,password);

if(!themail.sendout()) return false;

return true;

}

public static boolean sendandcc(string smtp,string from,string to,string copyto,string subject,string content,string username,string password) {

mail themail = new mail(smtp);

themail.setneedauth(true); //需要验证

if(!themail.setsubject(subject)) return false;

if(!themail.setbody(content)) return false;

if(!themail.setto(to)) return false;

if(!themail.setcopyto(copyto)) return false;

if(!themail.setfrom(from)) return false;

themail.setnamepass(username,password);

if(!themail.sendout()) return false;

return true;

}

public static boolean send(string smtp,string from,string to,string subject,string content,string username,string password,string filename) {

mail themail = new mail(smtp);

themail.setneedauth(true); //需要验证

if(!themail.setsubject(subject)) return false;

if(!themail.setbody(content)) return false;

if(!themail.addfileaffix(filename)) return false;

if(!themail.setto(to)) return false;

if(!themail.setfrom(from)) return false;

themail.setnamepass(username,password);

if(!themail.sendout()) return false;

return true;

}

public static boolean sendandcc(string smtp,string from,string to,string copyto,string subject,string content,string username,string password,string filename) {

mail themail = new mail(smtp);

themail.setneedauth(true); //需要验证

if(!themail.setsubject(subject)) return false;

if(!themail.setbody(content)) return false;

if(!themail.addfileaffix(filename)) return false;

if(!themail.setto(to)) return false;

if(!themail.setcopyto(copyto)) return false;

if(!themail.setfrom(from)) return false;

themail.setnamepass(username,password);

if(!themail.sendout()) return false;

return true;

}

}

我要举报

如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!

→点此我要举报以上信息!←

推荐资讯

大家都在看

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值