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("[email protected]"));
Transport transport = session.getTransport();
transport.connect("smtp.163.com",25,"z_p_u18064117557","**********");
transport.sendMessage(msg,new Address[]{new InternetAddress("[email protected]")});
//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("[email protected]"));
msg.setSubject("hello");
// msg.setText("hello");
msg.setContent("<h1>胡浩</h1>","text/html;charset=gbk");
msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected]"));
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://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("中国")+"<[email protected]>"));
msg.setContent(mp);
msg.setRecipient(RecipientType.BCC, new InternetAddress("[email protected]"));
msg.saveChanges();
Transport.send(msg, InternetAddress.parse("[email protected]"));
}
}