中易网

如何利用java发送163邮件,最好不要javamail

答案:1  悬赏:50  
解决时间 2021-02-28 02:20
如何利用java发送163邮件,最好不要javamail
最佳答案
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; // 系统属性
// smtp认证用户名和密码
private String username;
private String password;
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象


public Mail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}


public void setSmtpHost(String hostName) {
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
}


public boolean createMimeMessage() {
try {
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
} catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}

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) {
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) {
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=utf-8");
mp.addBodyPart(bp);

return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}


public boolean addFileAffix(String 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) {
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));
// 如果抄送人为null  不添加抄送人
if(mimeMsg.getRecipients(Message.RecipientType.CC) != null)
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);
e.printStackTrace();
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;
}

public static void main(String[] args) {
//// SMTP服务器
String smtp = "xxx";
// 发信人
String from = "xxx";
String to = "xxx";
String subject = "xxx";
String content = "xxx";
String username = "xxx";
String password = "xxx";
Mail.send(smtp, from, to, subject, content, username, password);
}
}这个是我现在在用的

追问。。。。都说了不要用javamail或者告诉我javamail咋弄追答不知道你指得javamail是引用的javax.mail包吗?
你把我的代码复制过去 只需要改main方法那几个字符串定义就可以用了
smtp 邮箱服务器 from 发件人 to 收件人 subject 主题 content 内容 username 用户名 password 密码追问我应用不了啊我没有像你安装了javamail也用不了追答http://download.csdn.net/download/yufengzuolao/6688033 下个javax.mail.jar就好了 或者你百度自己找个追问然后呢
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
为什么男人一靠近我就觉得很厌恶
花海之滨形象设计沙龙(仁寿美格店)地址在什么
光晕4航向黎明号 短片中,那个Laski的女友看
电脑什么单机游戏好玩?最好是打怪升级的
b站番剧显示,非常抱歉本视频不能播放,黑屏
帮我算算我的五行,2003年3月15日出生,几点
蚌埠市城市管理举报中心地址在什么地方,想过
三人行姐妹网名四字
郑州 蚕丝被 生产企业是哪家、
炉石传说,圣骑士该带什么法术,什么谦逊忏悔
我没天天早上醒来嘴干舍苦,舍头还拉不动帆似
手机一开机图标模糊怎么回事?
穿越火线的蹲下按键要怎么换成别的按键
歌词中有一句现在离开的你。 韩语歌
韩优韩国商品超市地址在哪,我要去那里办事
推荐资讯
赛尔号谱尼第一封印怎么打
交通运输部南海救助局在哪里?
引产后多久可以做有氧健身操
非经期出血,并且经期大出血是怎么回事?
晨曦美发店地址有知道的么?有点事想过去
肾上腺长瘤子在保险范围内吗?现在还不知道是
我想看我们骄傲的青春这部小说怎么才能免费看
如图求大神!这是手机优酷下载的一段视频,如
1970年农历8月初七出生在2016年的运势
带有神字的词语。5个
冀州市金丰工矿胶管有限责任公司我想知道这个
盐湖区运城网虫快捷酒店(一部)在哪里啊,我有
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?