java 发送邮件需要开启什么服务
关注:149 答案:2 mip版
解决时间 2021-02-04 10:01
提问者美人如画皮
2021-02-03 14:35
java 发送邮件需要开启什么服务
最佳答案
二级知识专家臸釪樶初
2021-02-03 14:49
需要开启SMTP协议
全部回答
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;
}
}
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
→点此我要举报以上信息!←
推荐资讯
大家都在看