有人可以提供一下吗?
一个接收,发送EMAIL的JAVA程序.
1条回答 默认 最新
- wanghaolovezlq 2009-06-08 20:11关注
[code="java"]
package cn.itcast.javamail2;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 Demo1 {
/** * @param args add by zxx ,Feb 5, 2009 */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub 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.setFrom(new InternetAddress("lili@sohu.com")); Transport transport = session.getTransport(); transport.connect("smtp.sina.com", 25, "itcast_test", "123456"); transport.sendMessage(msg, new Address[]{new InternetAddress("itcast_test@sohu.com")}); //transport.send(msg,new Address[]{new InternetAddress("itcast_test@sohu.com")}); transport.close(); }
}
[/code]
[code="java"]
package cn.itcast.javamail2;import java.io.FileInputStream;
import java.util.Properties;import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;public class Demo2 {
/** * @param args add by zxx ,Feb 5, 2009 */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.host", "smtp.sina.com"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("itcast_test","123456"); } } ); session.setDebug(true); /*Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("itcast_xxx@sina.com")); msg.setSubject("中文主题"); msg.setRecipients(RecipientType.TO, InternetAddress.parse("itcast_test@sina.com,itcast_test@sohu.com")); msg.setContent("<span style='color:red'>中文呵呵呵</span>", "text/html;charset=gbk"); Transport.send(msg);*/ Message msg = new MimeMessage(session,new FileInputStream("resouce\\demo3.eml")); Transport.send(msg,InternetAddress.parse("itcast_test@sohu.com")); }
}
[/code]
[code="java"]
package cn.itcast.javamail2;import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
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 Demo3 {
/** * @param args add by zxx ,Feb 5, 2009 */ public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Session session = Session.getInstance(new Properties()); MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("itcast") + "\" <itcast_test@sina.com>")); msg.setSubject("你们的Java培训真的是最牛的吗?"); msg.setReplyTo(new Address[]{new InternetAddress("lili@126.com")}); msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("黎活明") + " <llm@itcast.cn>," + MimeUtility.encodeText("张孝祥") + " <zxx@itcast.cn>")); MimeMultipart msgMultipart = new MimeMultipart("mixed"); msg.setContent(msgMultipart); MimeBodyPart attch1 = new MimeBodyPart(); MimeBodyPart attch2 = new MimeBodyPart(); MimeBodyPart content = new MimeBodyPart(); msgMultipart.addBodyPart(attch1); msgMultipart.addBodyPart(attch2); msgMultipart.addBodyPart(content); DataSource ds1 = new FileDataSource( "resource\\Java培训.txt" ); DataHandler dh1 = new DataHandler(ds1 ); attch1.setDataHandler(dh1); attch1.setFileName( MimeUtility.encodeText("java培训.txt") ); DataSource ds2 = new FileDataSource( "resource\\slogo.gif" ); DataHandler dh2 = new DataHandler(ds2 ); attch2.setDataHandler(dh2); attch2.setFileName("slogo.gif"); MimeMultipart bodyMultipart = new MimeMultipart("related"); content.setContent(bodyMultipart); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart gifPart = new MimeBodyPart(); bodyMultipart.addBodyPart(htmlPart); bodyMultipart.addBodyPart(gifPart); DataSource gifds = new FileDataSource( "resource\\logo.gif" ); DataHandler gifdh = new DataHandler(gifds); gifPart.setDataHandler(gifdh); gifPart.setHeader("Content-Location", "http://www.itcast.cn/logo.gif"); htmlPart.setContent("你们的Java培训真的是最牛的吗?大家都这么说,我想跟你们比试一下!这可是我自己用程序生成和发送的邮件哦!<img src='http://www.itcast.cn/logo.gif'>" , "text/html;charset=gbk"); msg.saveChanges(); OutputStream ips = new FileOutputStream("resource\\demo3.eml"); msg.writeTo(ips); ips.close(); }
}
[/code]
解决 无用评论 打赏 举报
悬赏问题
- ¥60 求一个c语言Linux下的项目代码
- ¥15 kill GPU process : no such process
- ¥50 TF卡写入时断电数据错乱的量会是多少?
- ¥15 解释hlm多层次线性回归
- ¥115 the HDL complexity tool
- ¥15 opencv腐蚀操作
- ¥15 grbl,限位开关在触发状态,怎么进行自动复位
- ¥15 matlab无法读取mat文件,如何解决?
- ¥15 51单片机读写24C02
- ¥15 win系统下做一个开机自动最大化运行某应用程序的执行文件