163邮件发送工具类附加测试类

本文介绍了一个用于163邮箱发送邮件的工具类,该工具类能够实现发送带有附件、文本内容及抄送功能的邮件。文章详细描述了如何设置163邮箱的授权码和主机,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1.此工具类为163邮箱发送邮件的工具类,可以实现发送附件、文本、抄送等基本功能。

  2.关键在于设置163 的 授权码 和主机。

/*
 * Copyright(C) 2000-2011 THS Technology Limited Company, http://www.ths.com.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ths.project.sysweb.sendemil;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.URLDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
import javax.mail.internet.MimeUtility;

/**
 *
 *
 * @author xinkun
 * @since 2018-9-20
 */
public class SendEmailUtil {
		
	private static String from ="1694669630@163.com"; // 发件人邮箱地址(还原添加本人QQ讨论)
	private static String user = "1694669630@163.com"; // 发件人称号,同邮箱地址
	private static String password = "****"; // 发件人邮箱客户端授权码
	private static String host ="smtp.163.com";
 
	/**
	 * 
	 * @param to 发送人123@qq.com,234@263.net
	 * @param text 正文
	 * @param title 标题
	 * @param fileList 附件 此处的list 包含 附件url 和附件名称 附件通过链接直接可以访问到附件 
	 */
	/* 发送验证信息的邮件 */
	public static boolean sendMail(String to, String text, String title,List<Map<String,Object>> fileList) {
		Properties props = new Properties();
		props.setProperty("mail.smtp.host", host); // 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
		props.put("mail.smtp.host", host); // 需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
		props.put("mail.smtp.auth", "true"); // 用刚刚设置好的props对象构建一个session
		Session session = Session.getDefaultInstance(props); // 有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
																// 用(你可以在控制台(console)上看到发送邮件的过程)
		session.setDebug(false); // 用session为参数定义消息对象  关闭控制台日志
		MimeMessage message = new MimeMessage(session); // 加载发件人地址
		try {
			message.setFrom(new InternetAddress(from));
			message.setRecipients(Message.RecipientType.TO, new InternetAddress().parse(to)); // 加载收件人地址
			message.setRecipients(Message.RecipientType.CC, new InternetAddress().parse(user)); // 抄送自己
			message.setSubject(title); // 加载标题
			Multipart multipart = new MimeMultipart(); // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
			BodyPart contentPart = new MimeBodyPart(); // 设置邮件的文本内容
			contentPart.setContent(text, "text/html;charset=utf-8");
			multipart.addBodyPart(contentPart);
			
			
			//为邮件添加多个附件
	        MimeBodyPart attachment = null;
	        URL url = null;
	        try {
		        if(fileList !=null && fileList.size()>0){
	        	   for(int i=0;i<fileList.size();i++){
	        		  String filename= fileList.get(i).get("FILENAME").toString();//文件名称	
	        		  String filepath= (String) fileList.get(i).get("FILEPATH");
	        			  try {
	        				  url=new URL(filepath);
	        			  } catch (MalformedURLException e) {
	        				  // TODO Auto-generated catch block
	        				  e.printStackTrace();
	        			  }
	        			  DataSource ds=new URLDataSource(url);
	        			  attachment = new MimeBodyPart();
	        			  attachment.setDataHandler(new DataHandler(ds));
	        			  attachment.setFileName(MimeUtility.encodeText(filename));
	        			  multipart.addBodyPart(attachment);	        		  
	        	   }
		        }
	        } catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			}
	         
			message.setContent(multipart);//将multipart对象放到message中
			message.saveChanges(); // 保存变化
			Transport transport = session.getTransport("smtp"); // 连接服务器的邮箱
			transport.connect(host, user, password); // 把邮件发送出去
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
 
	public static void main(String[] args) { // 做测试用
		SendEmailUtil ds = new SendEmailUtil();
		List<Map<String,Object>> filelist=new ArrayList<Map<String,Object>>();
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("FILENAME", "测试");
		map.put("FILEID", "6E5CFD87-293F-44D1-9FDE-1A2786EADF20");
		filelist.add(map);
		ds.sendMail("123654@QQ.cn", "你好,这是一封测试邮件,无需回复。", "测试邮件标题",filelist);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值