java发送邮件需要打开什么服务?
:149回答关注:2mip版
解决时间2021-02-04 10:01
提问者的美人如画皮
2021-02-03 14:35
java发送邮件需要打开什么服务?
最佳答案
二级知识专家
2021-02-03 14:49
必须打开SMTP协议
全部回答
一楼为了你贬低了我自己
2021-02-03 16:12
要使用javamail发送邮件,需要两个包: mail.jar和activtion.jar。
该类实现了更完整的电子邮件发送功能,如以html格式发送、添加附件和添加抄送。 下面是具体代码。
packagecn.cgw.util.mail;
importjava.util.properties;
import javax.activation.data handler;
import javax.activation.filedata source;
importjavax.mail.address;
importjavax.mail.bodypart;
importjavax.mail.message;
importjavax.mail.multipart;
importjavax.mail.session;
importjavax.mail.transport;
import javax.mail.internet.internetaddress;
import javax.mail.internet.mime body part;
import javax.mail.internet.mime消息;
import javax.mail.internet.mime multipart;
公共类邮件{
privatemimemessagemimemsg; //mime邮件对象
私有会话会话; //邮件会话对象
私有属性处理器; //系统属性
私密性booleanneedauth=false; //smtp是否需要认证
//smtp认证用户名和密码
私有用户名称;
隐私密码;
私有多点模式; 添加//multipart对象、邮件内容、标题、附件等以生成mime消息对象
公共邮件(字符串SMTP )。
设置smtphost (SMTP;
创建mime消息(;
}
publicvoidsetsmtphost (字符串主机名称)。
system.out.println (设置系统属性: mail.smtp.host=' hostname );
if (props==空)
props=system.getproperties (; //获取系统属性对象
props.put('mail.SMTP.host ',hostname ); 设置smtp主机
}
publicbooleancreatemimemessage ()。
{
try{
system.out.println ('已准备好检索邮件会话对象!' );
session=session.getdefaultinstance (props,null ); //获取邮件会话对象
}
catch(exceptione ) {
system.err.println ('获取邮件会话对象时出错!' e;
返回假;
}
system.out.println ('已准备好创建mime邮件对象!' );
try{
mime msg=new mime消息(session; 创建mime邮件对象
mp=newmimemultipart;
返回真;
}catch(exceptione ) {
system.err.println ('创建mime邮件对象失败!' e;
返回假;
}
}
>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;
}
}
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
→点此我要举报以上信息!←
推荐资讯
大家都在看