首页 > 代码库 > JavaMail发送邮件小例子

JavaMail发送邮件小例子

package Demo1;


import java.util.Properties;


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


public class JavaMailDemo1 {



 public  static void main(String args[]) throws Exception
 {
  Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(props);
session.setDebug(true);

Message msg = new MimeMessage(session);
msg.setText("你好吗?");
msg.setSubject("hello");
msg.setFrom(new InternetAddress("z_p_u18064117557@163.com"));

Transport transport = session.getTransport();
transport.connect("smtp.163.com",25,"z_p_u18064117557","**********");
transport.sendMessage(msg,new Address[]{new InternetAddress("z_p_u18064117557@163.com")});
//transport.send(msg);


transport.close();
   
}     

}




package Demo1;


import java.util.Properties;


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


public class JavaMailDemo2 {
 
public static void main(String[] args)  throws Exception  {

Properties props=new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.sohu.com");
Session session=Session.getInstance(props,new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication()
      {
      return new  PasswordAuthentication("z_p_u", "*********");
      }
});
session.setDebug(true);

Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress("z_p_u@sohu.com"));
msg.setSubject("hello");
// msg.setText("hello");
msg.setContent("<h1>胡浩</h1>","text/html;charset=gbk");
msg.setRecipients(RecipientType.TO, InternetAddress.parse("z_p_u@sohu.com"));


Transport.send(msg);
}


}






package Demo1;


import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;


import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
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;


public class JavaMailDemo3 {


   public static void main(String[] args) throws Exception {
    Properties props=new  Properties();
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.transport.proptocol", "smtp");
    props.setProperty("mail.host", "smtp.sohu.com");
Session session=Session.getInstance(props,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("z_p_u", "********");
}
});
    session.setDebug(true);
   
    Message msg=new MimeMessage(session);
   
    MimeMultipart mp= new MimeMultipart("related");
    MimeBodyPart html=new MimeBodyPart();
    MimeBodyPart gif=new MimeBodyPart();
    mp.addBodyPart(gif);
    mp.addBodyPart(html);
   
   
   
    html.setContent("<h1 style=‘color:red‘>你好</h1><img src=http://www.mamicode.com/‘http://avatar.csdn.net/F/B/6/1_u013516966.jpg‘/>", "text/html;charset=gbk");
    //gif.attachFile(new File("img/3.png"));
    //gif.setContent("<h1 style=‘color:red‘>你好</h1>", "text/html;charset=gbk");
   
   
    DataSource ds=new FileDataSource(new File("img/3.png"));
DataHandler dh=new DataHandler(ds);
gif.setDataHandler(dh);
gif.setHeader("Content-Location", "http://avatar.csdn.net/F/B/6/1_u013516966.jpg");
gif.setFileName(MimeUtility.encodeText("图片.png"));
   
msg.setFrom(new InternetAddress(MimeUtility.encodeText("中国")+"<z_p_u@sohu.com>"));

msg.setContent(mp);

msg.setRecipient(RecipientType.BCC, new InternetAddress("z_p_u@sina.cn"));

msg.saveChanges();



Transport.send(msg, InternetAddress.parse("z_p_u@sohu.com"));
}


}

JavaMail发送邮件小例子