1、构建一个继承陆派自javax.mail.Authenticator的具体类,并重写里面的getPasswordAuthentication()方法。此类是用作登录校验的,以确保你对该邮箱有发送邮件的权利。
2、构建一个properties文件,该文件中存放SMTP服务器地址等参数。
3、通过构建的properties文件和javax.mail.Authenticator具体类来创建一个javax.mail.Session。Session的创建,就相当于登录邮箱一样哗衡。剩下的自然就是新建邮件。
4、构建邮件内容,一般是javax.mail.internet.MimeMessage对象,并指定早芦贺发送人,收信人,主题,内容等等。
5、使用javax.mail.Transport工具类发送邮件。
方法:1.前提准备工作:
首先,邮件的发送方要开启POP3 和SMTP服务--即发送qq邮件的账号要开启POP3 和SMTP服务
2.开启方法:
登陆qq邮箱
3.点击 设置
4.点击—-账户
5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启
6.送短信 —–点击确定
7.稍等一会,很得到一个授权码! –注意:这个一定要记住,一唤瞎会用到
8.点击保存修改 —OK 完成
9.java 测试代码:
package cn.cupcat.test
import java.util.Properties
import javax.mail.Message
import javax.mail.MessagingException
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.AddressException
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMessage.RecipientType
public class SendmailUtil {
public static void main(String[] args) throws AddressException,MessagingException {
Properties properties = new Properties()
properties.put("mail.transport.protocol", "smtp")// 连接协议
properties.put("mail.smtp.host", "smtp.qq.com")// 主机名
properties.put("mail.smtp.port", 465)// 端口号
properties.put("mail.smtp.auth", "true"桐仔)
properties.put("mail.smtp.ssl.enable", "true")/和轮空/设置是否使用ssl安全连接 ---一般都使用
properties.put("mail.debug", "true")//设置是否显示debug信息 true 会在控制台显示相关信息
//得到回话对象
Session session = Session.getInstance(properties)
// 获取邮件对象
Message message = new MimeMessage(session)
//设置发件人邮箱地址
message.setFrom(new InternetAddress("[email protected]"))
//设置收件人地址message.setRecipients(RecipientType.TO,new InternetAddress[] { new InternetAddress("[email protected]") })
//设置邮件标题
message.setSubject("这是第一封Java邮件")
//设置邮件内容
message.setText("内容为: 这是第一封java发送来的邮件。")
//得到邮差对象
Transport transport = session.getTransport()
//连接自己的邮箱账户
transport.connect("[email protected]", "vvctybgbvvophjcj")//密码为刚才得到的授权码
//发送邮件transport.sendMessage(message, message.getAllRecipients())
}
}
10.运行就会发出邮件了。。。。
下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的,你们测试的时候,可以修改能你们自己的邮箱。最后,祝你也能成功,如果有什么问题,可以一起讨论!
注意事项
得到的授权码一定要保存好,程序中要使用
要两个java文件 还有一个mail.jar是不是只能用javamail谁也不敢说第一个:
public class Constant {
public static final String mailAddress ="用户名@163.com"
public static final String mailCount ="用户名"
public static final String mailPassword ="密码"
public static final String mailServer ="smtp.163.com"
//pukeyouxintest,
}
第二个:
import java.util.Date
import java.util.Properties
import javax.mail.Message
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
public class SendMail {
/**
* 发送简单邮件
* @param str_from:发件人地址
* @param str_to:收件人地址
* @param str_title:邮件标题
* @param str_content:邮件正文
*/
public static void send(String str_from,String str_to,String str_title,String str_content) {
// str_content="<a href='www.163.com'>html元素</a>"//for testing send html mail!
try {
//建立邮件会话
Properties props=new Properties()//用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,
//存储发送邮件服务器的信息
props.put("mail.smtp.host",Constant.mailServer)
//同时通过验证
props.put("mail.smtp.auth","true")
//根据属性新建一个邮件会话
Session s=Session.getInstance(props)
s.setDebug(true)//有他会打印一些调试信息。
//由邮件会话新建一个消息对象
MimeMessage message=new MimeMessage(s)
//设置邮件
InternetAddress from= new InternetAddress(str_from)//[email protected]
message.setFrom(from)//设置发件人举唯的地址
//
// //设置收件人,并设置其接收类型为TO
InternetAddress to=new InternetAddress(str_to)//[email protected]
message.setRecipient(Message.RecipientType.TO, to)
//设置标题
message.setSubject(str_title)//java学习
//设置信件内容
// message.setText(str_content)//发送文本邮件 //你好吗?
message.setContent(str_content, "text/htmlcharset=gbk")//发送HTML邮件 //<b>你好</b><br><p>大家好</p>
//设置发信时间
message.setSentDate(new Date())
//存储咐答中邮件信息
message.saveChanges()
//发送邮件
Transport transport=s.getTransport("smtp")
//衡山以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect(Constant.mailServer,Constant.mailCount,Constant.mailPassword)
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message,message.getAllRecipients())
transport.close()
} catch (Exception e) {
e.printStackTrace()
}
}
public static void main(String[] args) {
//测试用的,你吧你想写的内容写上去就行
send(Constant.mailAddress,"收件人邮箱","标题","<b>内容</b>")
}
}
然后把mail.jar导入,就可以了,我用的是163 的,其他的吧相应的服务器改一下就行了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)