⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 senderwithsmtpver.java

📁 借鉴使用JAVA发送邮件例子程序,已经测试通过,感觉还不错
💻 JAVA
字号:
package src;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* @author Bobbie
*/
public class SenderWithSMTPVer
{
	String host="";
	String user="";
	String password="";

	public void setHost(String host)
	{
		this.host=host;
	}

	public void setAccount(String user,String password)
	{
		this.user=user;
		this.password=password;
	}

	/**
	 * @param from		发件人的地址
	 * @param to		收件人地址
	 * @param subject	邮件标题
	 * @param content	邮件正文
	 */
	public void send(String from,String to,String subject,String content)
	{
		Properties props = new Properties();
		props.put("mail.smtp.host", host);//指定SMTP服务器
		props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
		try
		{
			Session mailSession = Session.getDefaultInstance(props);
	  
			mailSession.setDebug(true);//是否在控制台显示debug信息
	  
			
			Message message=new MimeMessage(mailSession);
		    message.setFrom(new InternetAddress(from));//发件人
		    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//收件人
		  
		    message.setSubject(subject);//邮件主题
		    message.setContent(content,"text/html;charset=GBK");  //若发送网页格式的邮件内容,使用这行代码
//		    message.setText(content);//邮件内容		若不以网页格式发送邮件则注释上一行,取消这行注释
		    message.saveChanges();
		  
		    Transport transport = mailSession.getTransport("smtp");
		    transport.connect(host, user, password);
		    transport.sendMessage(message, message.getAllRecipients());
		    transport.close();
		}catch(Exception e)
		{
			System.out.println(e);
		}
 
	}
	
	/**
	 * 读取properties文件信息
	 * @param filePath
	 * @return
	 */
	public EmailInfo getEmailInfo(String filePath){
		EmailInfo emailInfo=new EmailInfo();
		try{
			Properties pro=new Properties();
			FileInputStream fileInput=new FileInputStream(filePath);
			pro.load(fileInput);
			fileInput.close();
			
			emailInfo.setHost(pro.getProperty("host"));
			emailInfo.setTo(pro.getProperty("to"));
			emailInfo.setFrom(pro.getProperty("from"));
			emailInfo.setUserName(pro.getProperty("name"));
			emailInfo.setPassWord(pro.getProperty("password"));
			emailInfo.setTitle(pro.getProperty("c_title"));
			emailInfo.setContent(pro.getProperty("c_content"));
			emailInfo.setS_title(pro.getProperty("s_title"));
			emailInfo.setS_content(pro.getProperty("s_content"));
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return emailInfo;
	}

	public static void main(String args[])
	{
		SenderWithSMTPVer sm=new SenderWithSMTPVer();

		EmailInfo emailInfo=sm.getEmailInfo("info.properties");
		sm.setHost(emailInfo.getHost());//指定要使用的邮件服务器 如:mail.hyrt.com.cn或者smtp.163.com
		sm.setAccount(emailInfo.getUserName(),emailInfo.getPassWord());//指定帐号和密码
		
		String c_title=emailInfo.getTitle();
		String c_content=emailInfo.getContent();
		String s_title=emailInfo.getS_title();
		String s_content=emailInfo.getS_content();
		String to="qhs7230@sina.com.cn";        //注册会员邮箱地址,作为一参数传入即可
		
		try{
			c_title=new String(c_title.getBytes("ISO-8859-1"),"gb2312");
			c_content=new String(c_content.getBytes("ISO-8859-1"),"gb2312");
			s_title=new String(s_title.getBytes("ISO-8859-1"),"gb2312");
			s_content=new String(s_content.getBytes("ISO-8859-1"),"gb2312");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	    /*
	     * @param String 发件人的地址
	     * @param String 收件人地址
	     * @param String 邮件标题
	     * @param String 邮件正文
	    */
//		为注册会员发送邮件
		sm.send(emailInfo.getFrom(),to,c_title,c_content);
//		为名索管理员发送邮件
		sm.send(emailInfo.getFrom(),emailInfo.getTo(),s_title,s_content);
//		sm.send("qihs@hyrt.com.cn","qhs7230@sina.com.cn","测试发送邮件","<html><body><h1>下面的,你们好吗?</h1><a href='http://www.mingsuo.com'>test</a></body></html>");
	}

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -