使用java发送邮箱

发布时间:2023-10-02 点击:95
使用java发送邮箱 以qq邮箱为例说明发送邮箱
导包
<!– 发送邮件 –> <dependency> <groupid>com.sun.mail</groupid> <artifactid>javax.mail</artifactid> <version>1.6.0</version> </dependency> </dependencies> qq邮箱开启pop3/smtp服务
设置-.>账户->pop3/imap/smtp/exchange/carddav/caldav服务
开启pop3/smtp服务
生成授权码后将授权码保存,后面会用
代码
package com.spring.until; import java.security.generalsecurityexception; import java.util.properties; import javax.mail.authenticator; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimemessage; import com.sun.mail.util.mailsslsocketfactory; public class sendemail { public static boolean send(string email,string title,string body) throws generalsecurityexception { // 收件人电子邮箱 string to = email; // 发件人电子邮箱 string from = propertiesuntil.properties(“account”); // 指定发送邮件的主机为 smtp.qq.com string host = “smtp.qq.com”; //qq 邮件服务器 // 获取系统属性 properties properties = system.getproperties(); // 设置邮件服务器 properties.setproperty(“mail.smtp.host”, host); properties.put(“mail.smtp.auth”, “true”); mailsslsocketfactory sf = new mailsslsocketfactory(); sf.settrustallhosts(true); properties.put(“mail.smtp.ssl.enable”, “true”); properties.put(“mail.smtp.ssl.socketfactory”, sf); // 获取默认session对象 session session = session.getdefaultinstance(properties,new authenticator(){ public passwordauthentication getpasswordauthentication() { return new passwordauthentication( propertiesuntil.properties(“account”), propertiesuntil.properties(“password”)); //发件人邮件用户名、密码 } }); try{ // 创建默认的 mimemessage 对象 mimemessage message = new mimemessage(session); // set from: 头部头字段 message.setfrom(new internetaddress(from)); // set to: 头部头字段 message.addrecipient(message.recipienttype.to, new internetaddress(to)); // set subject: 头部头字段 message.setsubject(title); // 设置消息体 message.settext(body); // 发送消息 transport.send(message); return true; }catch (messagingexception mex) { return false; } } }
propertiesuntil.properties(“account”) 发送人邮箱账号
propertiesuntil.properties(“password”) 发送人邮件密码,注意是刚刚生成的
授权码
string email,string title,string body 三个参数分别为收件人邮箱,标题,正文
西部数码优质企业邮箱服务商,提供安全稳定,简单易用,高性价比的企业邮箱。按需自由定制,不限空间,极速收发,能够满足用户对企业邮箱的不同需求。多种反垃圾邮件算法,99.9%精准度,智能过滤,减少垃圾邮件干扰。支持小程序收发邮件,随时随地移动办公。而且价格实惠,还可以免费试用,7×24小时专业团队服务支持!
高性价比企业邮箱开通链接:https://www.west.cn/services/mail/


国双:紧跟新基建,推动大数据中心健康发展
有限流量(metered)服务器和无限流量(unmetered)服务器真...
非法信息监测结果为百度搜索结果
域名备案要注意哪些问题?
独享虚拟主机好还是云服务器
网站网址的应用背景和应用历程
云服务器如何挂机器人
案例分析PHP Web表单生成器