JavaEmail-邮箱
package service; import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.Message;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage; import java.util.Properties;
public class EmailBox { //根据需要输入参数,我这里用到两个
public String mail(String a , String b) { // JDK9以上或JDK6以下使用mail.jar包不加JAF的activation.jar包会抛出该错误!JDK6以上不需要加该jar包;
String email = a;
String num = b;
//随机数拼成验证码
// for (int i = 0; i < 6; i++) {
// int n = (int) (Math.random() * 10);
// num = num + Integer.toString(n);
// }
Properties prop = new Properties();
//// 开启debug调试,以便在控制台查看
// prop.setProperty("mail.debug", "true");
// 设置邮件服务器主机名,这里用QQ邮箱
prop.setProperty("mail.host", "smtp.qq.com");
// 发送服务器需要身份验证
prop.setProperty("mail.smtp.auth", "true");
// 发送邮件协议名称
prop.setProperty("mail.transport.protocol", "smtp"); try {
// 开启SSL加密,否则会失败
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true");
prop.put("mail.smtp.ssl.socketFactory", sf);
// 创建session
Session session = Session.getInstance(prop);
// 通过session得到transport对象
Transport ts = session.getTransport();
// 连接邮件服务器:邮箱类型,帐号,授权码代替密码(更安全)
ts.connect("smtp.qq.com", "这里填写QQ帐号", "这里填写QQ安全登陆码");//后面的字符是授权码,用qq密码反正我是失败了(用自己的,别用我的,这个号是我瞎编的,为了。。。。)
// 创建邮件
MimeMessage message = createSimpleMail(session, email, num);
// 发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
return "发送成功!";
} catch (Exception e) {
e.printStackTrace();
return "发送失败!";
}
} public static MimeMessage createSimpleMail(Session session, String email, String num)
throws Exception {
// 创建邮件对象
MimeMessage message = new MimeMessage(session);
// 指明邮件的发件人
message.setFrom(new InternetAddress("这里填写我的QQ邮箱,例如xxxxxxxxx@qq.com"));
// 指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("这里填写你要发送的邮箱,如xxxxxxx@qq.com"));
// 邮件的标题
message.setSubject("这里填写要发送的邮件标题");
// 邮件的文本内容
message.setContent("这里填写要发送的邮件内容" , "text/html;charset=UTF-8");
// 返回创建好的邮件对象
return message;
}
}
JavaEmail-邮箱的更多相关文章
- JavaMail发送邮箱
package utils; import java.security.GeneralSecurityException; import java.util.Properties; import ja ...
- 使用JavaMail发送邮箱详解
package com.gqz.forfuture.email; import java.util.Date; import java.util.Properties; import javax.ma ...
- Xamarin+Prism小试牛刀:定制跨平台Outlook邮箱应用
通过本文你将学会如下内容: 1,如何使用Xamarin开发跨平台(Windows,Android,iOS)应用. 2,如何使用微软的登录界面登入Microsoft账号. 3,如何使用Outlook邮箱 ...
- Xamarin+Prism小试牛刀:定制跨平台Outlook邮箱应用(后续)
在[Xamarin+Prism小试牛刀:定制跨平台Outlook邮箱应用]里面提到了Microsoft 身份认证,其实这也是一大块需要注意的地方,特作为后续补充这些知识点.上章是使用了Microsof ...
- C#发送邮箱
之前自己从来没有做过发送邮箱的功能,前段时间项目需要,在找了很多帖子之后,终于实现了. 之后有整理了一下,写了一个类.直接给类传递信息,就可以发送了. 这里还需要说明的是,发送邮箱需要开通POP3/S ...
- iOS之判断手机号码、邮箱格式是否正确
//判断手机号码格式是否正确 + (BOOL)valiMobile:(NSString *)mobile{ mobile = [mobile stringByReplacingOccurren ...
- 技术笔记:Indy IdSMTP支持腾讯QQ邮箱邮件发送
1.腾讯QQ邮箱的授权码问题 因为腾讯邮箱折腾了个底朝天,其要搞什么授权码登录第三方客户端,否则会报这个错误: 'Error: 请使用授权码登录.详情请看: http://service.mail.q ...
- ★Kali信息收集~ 5.The Harvester:邮箱挖掘器
官网:http://www.edge-security.com 安装:apt-get install theHarvester 运行:终端输入 theharvester (小写) 用法+参数:(返回邮 ...
- Email系列(QQ邮箱 + 含附件的邮箱案例 + 项目实战)
平台之大势何人能挡? 带着你的Net飞奔吧! http://www.cnblogs.com/dunitian/p/4822808.html 邮箱系列:https://github.com/duniti ...
- 【代码笔记】iOS-验证手机号,邮箱,车牌号是否合法
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...
随机推荐
- m文件转换c代码
parametet.mclc; clear; load('src.mat') CZT_N = ; CZT_M = ; CZT_W = exp(-j*(*pi/)); CZT_A = exp(j**pi ...
- ExecuteReader()获得数据
ExecuteReader用于实现只进只读的高效数据查询.ExecuteReader:返回一个SqlDataReader对象,可以通过这个对象来检查查询结果,它提供了只进只读的执行方式,即从结果中读取 ...
- 106. Construct Binary Tree from Inorder and Postorder Traversal根据后中序数组恢复出原来的树
[抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assum ...
- linux下apt安装mysql导致mysql.user table is damaged
笔者在ubuntu下用 apt install mysql-server类似的命令安装mysql, 安装了最新版的mysql5.7,覆盖了操作系统内置的数据库mysql系统库. 最初启动mysql出错 ...
- Android抓取log日志过滤
前提:Android SDK已安装并配置环境变量 1.手机USB调试模式打开,连接PC 2.cmd窗口,执行adb logcat >log.log // 输出日志到一个log文件 或者执行a ...
- 现代编译原理--第六章(中间树 IR Tree 含源码)
(转载请表明出处 http://www.cnblogs.com/BlackWalnut/p/4559717.html ) 这一章,就虎书而言,理论知识点是及其少的,就介绍了为什么要有一个中间表示树 ...
- 嵌入Python | 调用Python模块中无参数的函数
开发环境 Python版本:3.6.4 (32-bit) 编辑器:Visual Studio Code C++环境:Visual Studio 2013 需求说明 在用VS2013编写的Win32程序 ...
- java 多线程 同步 观察者 并发集合的一个例子
//第一版 package com.hra.riskprice; import com.hra.riskprice.SysEnum.Factor_Type; import org.springfram ...
- Python中删除easy_install安装的包
网上查了一大圈,终于在官网上找到了.记一下,备忘...
- ScriptOJ-unique#89
一般做法 const unique = (arr) => { const result = arr.reduce((acc, iter) => { if(acc.indexOf(iter) ...