用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

电子邮件发送

2016-07-05 作者: NeoSteven举报

[java]代码库

package com.java;

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 Email {

	//复制该段代码后请自行更改发送邮件的邮箱地址和QQ邮箱POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务的授权码
	public static void main(String[] args) {

		//测试sendEmail()方法
		String state = sendEmail();
		System.out.println(state);
		
	}
	
	
	public static String  sendEmail() {
		
		String flag = "";
		
		//建立邮件会话
		  Properties pro = new Properties();
		  pro.put("mail.smtp.host","smtp.qq.com");//存储发送邮件的服务器
		  pro.put("mail.smtp.auth","true");  //通过服务器验证
		  
		  Session s =Session.getInstance(pro); //根据属性新建一个邮件会话
		  //s.setDebug(true);
		  
		  //由邮件会话新建一个消息对象
		  MimeMessage message = new MimeMessage(s);
		  
		  //设置邮件
		  InternetAddress fromAddr = null;
		  InternetAddress toAddr = null;
		  
		  try 
		  {
		   fromAddr = new InternetAddress(1033732560+"@qq.com");   //邮件发送地址
		   message.setFrom(fromAddr);         //设置发送地址
		   
		   toAddr = new InternetAddress("728006155@qq.com");       //邮件接收地址
		   message.setRecipient(Message.RecipientType.TO, toAddr);  //设置接收地址
		   
		   message.setSubject("xxxOA找回密码");   //设置邮件标题
	   
		   message.setContent(
			   "<div>" +
			   		"<br>" +
			   "</div>" +
			   "<div>" +
			   		"<img src="+"\""+"https://www.baidu.com/img/baidu_jgylogo3.gif"+"\""+">"+
			   		
		   			"<br>" +
		   		"</div>" +
			   		
			   "<div>" +
			   		"<includetail>&nbsp; 亲爱的用户:&nbsp;</includetail>" +
			   "</div>" +
			   "<div>" +
			   		"<includetail><br></includetail>" +
			   "</div>" +
			   "<div>" +
			   		"<includetail>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;您好,您的xxxxOA帐号密码重置为: <b><font color="+"\""+"#ff0000"+"\""+">"+"new password"+"</font></b>(此密码为临时密码),</includetail>" +
			   "</div>" +
			   "<div>" +
			   		"<includetail><br></includetail>" +
			   "</div>" +
			   "<div>" +
			   		"<includetail>&nbsp; 请登录后到我的账号信息中重新修改密码如果您对此通知有任何疑问,请联系管理员。</includetail>" +
			   "</div>" +
			   "<div>"+
			   		"<includetail><br></includetail>"+
			   "</div>" +
			   "<div>" +
			   		"<includetail>&nbsp;&nbsp;</includetail>" +
			   		"<span style="+"\""+"color: rgb(255, 0, 0); font-family: 微软雅黑, sans-serif; line-height: 1.5;"+"\""+">此信由支xxxOA系统发出,系统不接受回信,因此请勿直接回复。</span>" +
			   	"</div>" +
			   	"<div>" +
			   		"<br>" +
			   	"</div>" +
			   	"<div>" +
			   		"<span style="+"\""+"font-family: 微软雅黑, sans-serif;"+"\""+"></span>" +
			   	"</div>","text/html;charset=UTF-8");
		   		   
		   message.setSentDate(new Date()); //设置邮件日期
		   
		   message.saveChanges();    //保存邮件更改信息

		   Transport transport = s.getTransport("smtp");
		   transport.connect("smtp.qq.com", "1033732560@qq.com", "此处填写QQ邮箱的授权密码(并不是QQ密码)"); //服务器地址,邮箱账号,邮箱密码
		   transport.sendMessage(message, message.getAllRecipients());   //发送邮件
		   transport.close();//关闭
		   
		   return flag = "true";
		 
		  } 
		  catch (Exception e) 
		  {
		   e.printStackTrace();
		   flag = "false";//发送失败
		  }
		  
		  return flag;
		 } 
	}

<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>


[代码运行效果截图]


电子邮件发送


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...