java 发送邮件

转自:http://yzz9i.iteye.com/blog/1341357

其中如果出现classnotfoundexception,就要修改javaee,删除其中的mail包,导入自己的mail.jar即可。
注意如果使用其他邮箱,要将sina换为相应的内容,其中使用qq邮箱的时候,要注意,如果报错,
可能是因为你的QQ邮箱没有开通:
POP3/SMTP服务。
如果是550 Mailbox not found错误,那就是目标邮箱不存在。
/**
 * 
 */
package two;
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 Send {  
      
    private final String subject;  
    private final String to;  
    private final String neirong;  
      
      
    public Send(String t,String subt,String nr)  
    {  
        subject=subt;  
        to=t;  
        neirong=nr;  
    }  
    
    public static void main(String[] args) {
    	Send se = new Send("[email protected]", "title??????","contents?????");  
    	se.send(); 
	}
  
      
    public void send()  
    {  
          
        Properties props = new Properties();  
        //设置邮件服务器地址,连接超时时限等信息  
        props.put("mail.smtp.host", "smtp.sina.com");  
        props.put( "mail.smtp.auth",   "true");  
        props.put("mail.smtp.connectiontimeout", "10000");  
        props.put("mail.smtp.timeout", "10000");  
  
        //创建缺省的session对象  
        Session session = Session.getDefaultInstance(props, null);  
  
        //创建message对象  
        Message msg = new MimeMessage(session);  
  
        //设置发件人和收件人  
        try{//[email protected] paul5198  
            InternetAddress addressFrom = new InternetAddress("[email protected]");  
            msg.setFrom(addressFrom);  
            InternetAddress addressTo = new InternetAddress(to);  
              
            msg.setRecipient(Message.RecipientType.TO, addressTo);  //设置接收邮箱,可多个  
//msg.setRecipients(Message.RecipientType.TO, new Address[]{new InternetAddress("[email protected]"),new InternetAddress("[email protected]")});
            //设置邮件标题,中文编码  
             //subject = MimeUtility.encodeText(new String(subject.getBytes(), "GB2312"), "GB2312", "B");  
            msg.setSubject(subject);   
            msg.setText(neirong);   
            Transport   transport   =   session.getTransport( "smtp");  
            transport.connect("smtp.sina.com",   "[email protected]",   "xxx");  
            transport.sendMessage(msg,   msg.getAllRecipients());  
            transport.close();  
              
        }catch(Exception e)  
        {  
            e.printStackTrace();  
        }  
          
    }  
  
     
  
}  

 

 

demo:

 

package com.tch.test;

import java.text.SimpleDateFormat;



public class MySendMail {

	public static void main(String[] args) throws Exception {
		sendActiveMail("http://www.baidu.com","大哥","[email protected]");
	}
	
	public static boolean sendActiveMail(String activeAddress, String username,
			String toMail) throws Exception {

		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy年MM月dd日"); // 格式化当前系统日期
		String dateTime = dateFm.format(new java.util.Date());
		// 发送邮件主题
		String subject = "主题";
		// 发送邮件内容
		StringBuffer content = new StringBuffer();
		content.append("<table width='600' border='0'>");
		content.append("<tr><td>" + username.toLowerCase() + " 您好!</td></tr>");
		content.append("<tr><td>&nbsp;</td></tr>");
		content.append("<tr><td>感谢您注册xxx。</td></tr>");
		content.append("<tr><td>&nbsp;</td></tr>");
		content.append("<tr><td>请点击以下链接完成注册过程:</td></tr>");
		content.append("<tr><td><a href='" + activeAddress
				+ "' target='_blank'>" + activeAddress + "</a></td></tr>");
		content
				.append("<tr><td>(如果不能点击该链接地址,请复制并粘贴到浏览器的地址输入框中进行查看。)</td></tr>");
		content.append("<tr><td>&nbsp;</td></tr>");
		content.append("<tr><td>此邮件由xxx发出,请勿直接回复。</td></tr>");
		content.append("<tr><td>&nbsp;</td></tr>");
		content.append("<tr><td>xxx有限公司</td></tr>");
		content.append("<tr><td>" + dateTime + "</td></tr>");
		content.append("</table>");
		// 发送者别名
		String senderAlias = "xxx公司";
		return sendMail(subject, content.toString(), toMail, senderAlias);
	}
	
	public static boolean sendMail(String subject, String content, String toMail,
			String senderAlias) throws Exception {
		String host=null;
		//设置发件人的邮箱
		String from=null;
		//设置发件人邮箱的用户名
		String mailname=null;
		//设置发件人邮箱的密码
		String mailpassword=null;
		
		//获得配置文件参数
		host = "smtp.qq.com";	
		from = "[email protected]";	
		mailname = "[email protected]";		
		mailpassword = "eee";
				
		MailBean mb = new MailBean();
		mb.setHost(host);
		mb.setUsername(mailname); // 设置发件人邮箱的用户名
		mb.setPassword(mailpassword); // 设置发件人邮箱的密码
		mb.setFrom(from); // 设置发件人的邮箱
		mb.setTo(toMail); // 设置收件人的邮箱
		mb.setSubject(subject); // 设置邮件的主题		  
		mb.setContent(content); // 设置邮件的正文
		mb.setSenderAlias(senderAlias);//设置发送人别名
		
		SendMail sm = new SendMail();
		try {
			if (sm.sendMail(mb)) { // 发送邮件
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
}

 

你可能感兴趣的:(java,mail)