package email2;import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.net.*;
import javax.activation.*;
import javax.swing.*;public class Sender {
//收信人地址
public String msgRecipient = "收信人地址";
//主题
public String msgSubject = "Re:your mail"; //抄信人
// protected String message_cc="[email protected]"; //正文
public String msgText = "aaaaaaaaaaaaaaaaaaaaaaaaaa" +
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
"ccccccccccccccccccccccccccccccccc" + "dddddddddddddddddddd"; //Attachment
public String attachment="c:\\12.jpg"; //the file name
public String fileName="temp"; //javamail Session对象
protected Session session; //java message对象
protected Message mesg; //发送邮件给smtp服务器
public void doSend() {
//java mail 允许处理许多的属性Properities
Properties props = new Properties(); //SMTP服务器设置
props.put("mail.smtp.host", "smtp.sina.com.cn");
props.put("mail.smtp.auth", "true"); // smtp属性服务器是要验证的 // Setup authentication, get session
// SMTPAuthenticator auth =new SMTPAuthenticator();
//Session session = Session.getInstance(props, null); session = Session.getDefaultInstance(props, null);
session.setDebug(true); try {
//创建一个邮件
mesg = new MimeMessage(session); //From 来自Properties
mesg.setFrom(new InternetAddress("邮箱")); //To 地址
InternetAddress toAddress = new InternetAddress(msgRecipient);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
toAddress = new InternetAddress(msgRecipient1);
mesg.addRecipient(Message.RecipientType.TO, toAddress); //cc地址
//InternetAddress ccAddress=new InternetAddress(message_cc);
// mesg.addRecipient(Message.RecipientType.CC,ccAddress); //主题
mesg.setSubject(msgSubject); //创建消息主体
Multipart multipart=new MimeMultipart(); //正文
BodyPart msgBody=new MimeBodyPart();
msgBody.setText(msgText);
multipart.addBodyPart(msgBody); //粘贴附件
msgBody=new MimeBodyPart();
DataSource source=new FileDataSource(attachment);
msgBody.setDataHandler(new DataHandler(source));
msgBody.setFileName(fileName);
multipart.addBodyPart(msgBody); //加入消息
mesg.setContent(multipart); //发送邮件
Transport transport = session.getTransport("smtp");
transport.connect("smtp.sina.com.cn", "你用户名", "密码"); //设置用户和密码
transport.sendMessage(mesg, mesg.getRecipients(Message.RecipientType.TO));
//transport.send(mesg); //Transport.send(mesg);
//transport.sendMessage(mesg, mesg.getAllRecipients());
transport.close();
}
catch (Exception ex) { System.out.println(ex.toString()); }
} public static void main(String[] av) { Sender sm = new Sender();
sm.doSend();
}}
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.net.*;
import javax.activation.*;
import javax.swing.*;public class Sender {
//收信人地址
public String msgRecipient = "收信人地址";
//主题
public String msgSubject = "Re:your mail"; //抄信人
// protected String message_cc="[email protected]"; //正文
public String msgText = "aaaaaaaaaaaaaaaaaaaaaaaaaa" +
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" +
"ccccccccccccccccccccccccccccccccc" + "dddddddddddddddddddd"; //Attachment
public String attachment="c:\\12.jpg"; //the file name
public String fileName="temp"; //javamail Session对象
protected Session session; //java message对象
protected Message mesg; //发送邮件给smtp服务器
public void doSend() {
//java mail 允许处理许多的属性Properities
Properties props = new Properties(); //SMTP服务器设置
props.put("mail.smtp.host", "smtp.sina.com.cn");
props.put("mail.smtp.auth", "true"); // smtp属性服务器是要验证的 // Setup authentication, get session
// SMTPAuthenticator auth =new SMTPAuthenticator();
//Session session = Session.getInstance(props, null); session = Session.getDefaultInstance(props, null);
session.setDebug(true); try {
//创建一个邮件
mesg = new MimeMessage(session); //From 来自Properties
mesg.setFrom(new InternetAddress("邮箱")); //To 地址
InternetAddress toAddress = new InternetAddress(msgRecipient);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
toAddress = new InternetAddress(msgRecipient1);
mesg.addRecipient(Message.RecipientType.TO, toAddress); //cc地址
//InternetAddress ccAddress=new InternetAddress(message_cc);
// mesg.addRecipient(Message.RecipientType.CC,ccAddress); //主题
mesg.setSubject(msgSubject); //创建消息主体
Multipart multipart=new MimeMultipart(); //正文
BodyPart msgBody=new MimeBodyPart();
msgBody.setText(msgText);
multipart.addBodyPart(msgBody); //粘贴附件
msgBody=new MimeBodyPart();
DataSource source=new FileDataSource(attachment);
msgBody.setDataHandler(new DataHandler(source));
msgBody.setFileName(fileName);
multipart.addBodyPart(msgBody); //加入消息
mesg.setContent(multipart); //发送邮件
Transport transport = session.getTransport("smtp");
transport.connect("smtp.sina.com.cn", "你用户名", "密码"); //设置用户和密码
transport.sendMessage(mesg, mesg.getRecipients(Message.RecipientType.TO));
//transport.send(mesg); //Transport.send(mesg);
//transport.sendMessage(mesg, mesg.getAllRecipients());
transport.close();
}
catch (Exception ex) { System.out.println(ex.toString()); }
} public static void main(String[] av) { Sender sm = new Sender();
sm.doSend();
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货