网站首页 新闻首页 网页设计图形动画软件编程网站开发办公软件操作系统数据库网络技术认证考试范文资料黑客攻防 书籍教程 进入论坛

javamail 发邮件

http://www.diybl.com/ 2008-3-8  网络 点击:  [ 评论 ]
文章搜索:    【点击打包该文章】

        昨天公司说要自己要做一个整合的消息平台,其中就要求把邮件的功能整合进来,幸好网上的资料比较多能拿来参考,但是也有很多值得特殊说明的地方,比如说代理、SSL验证、IMAP协议等。

        下面我们先从发送邮件开始说起吧,如果邮件服务器需要身份验证,我们可以采用两种方式进行发送。

       方式一:

 



package com.yourcompany.mail;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class gmaiSend {
    
     
public static void main(String[] args) throws AddressException, MessagingException {
          Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
          
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
          
// Get a Properties object
          Properties props = System.getProperties();
          props.setProperty(
"mail.smtp.host""smtp.sohu.com");  //搜狐smtp服务器
          props.put(
"mail.smtp.auth""true"); //设置需要验证
          
final String username = "xxxxx"//邮箱用户名
          final String password = "xxxx";   //邮箱密码
          Session session = Session.getDefaultInstance(props, new Authenticator(){
              
protected PasswordAuthentication getPasswordAuthentication() {
                  
return new PasswordAuthentication(username, password);
              }
}
);
          
          session.setDebug(
true);

               
// -- Create a new message --
          Message msg = new MimeMessage(session);

          
// -- Set the FROM and TO fields --
          msg.setFrom(new InternetAddress(username + "@sohu.com"));  //发件人邮箱地址
          msg.setRecipients(Message.RecipientType.TO, 
            InternetAddress.parse(
"xxx@xxxxl.com",false)); //收件人邮箱地址
          msg.setSubject("Hello");
          msg.setText(
"How are you");
          msg.setSentDate(
new Date());
          Transport.send(msg);
          
          System.out.println(
"Message sent.");
         }




}

     

方式二:

 



package com.yourcompany.mail;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class example {

    
public static void main(String[] args) throws AddressException,
            MessagingException 
{
        Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
        
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        
// Get a Properties object
        Properties props = System.getProperties();
        props.setProperty(
"mail.smtp.host""smtp.sohu.com"); //搜狐邮箱服务器
        props.put(
"mail.smtp.auth""true"); //设置需要验证
        
final String username = "xxxxx"//用户名
        final String password = "xxxxx"//密码

        Session newSession 
= Session.getInstance(props);
        MimeMessage message 
= new MimeMessage(newSession);
        InternetAddress from 
= new InternetAddress("xxx@sohu.com"); // 发件人邮箱
        message.setFrom(from);
        InternetAddress sto 
= new InternetAddress("xxxx@xxx.com"); // 收件人邮箱
        message.setRecipient(Message.RecipientType.TO, sto);
        message.setRecipient(Message.RecipientType.CC, sto);
        message.setRecipient(Message.RecipientType.BCC, sto);
        message.setSubject(
"hello");
        message.setSentDate(
new Date());
        BodyPart mdp 
= new MimeBodyPart();
        mdp.setContent(
"how are you""text/html;charset=gb2312");
        Multipart mm 
= new MimeMultipart();
        mm.addBodyPart(mdp);
        message.setContent(mm);
        Transport transport 
= newSession.getTransport("smtp");
        transport.connect(
"smtp.sohu.com", username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

        System.out.println(
"Message sent.");
    }


}

 

方式一和二风格上不是很统一,都是临时该的,由于想把代理等内容说的更清楚些,有些内容是拿出来单独讲,后面完成所有内容时,附上测试过的代码,所以如果上面代码测试不通过,请您别骂我:)推荐采用方式二,因为方式二可以发送发个邮件,可以应用在群发的目的上,



欢迎光临DIY部落,点击这里查看更多文章教程   【点击打包该文章】
如果图片或页面不能正常显示请点击这里 站内搜索:   

文章评论

请您留言

 

最新新闻