2段几乎全部相同的代码~第一段代码生成的是正常的邮件~
而第二段代码生成的内容全在附件里
实在搞不清是怎么回事第一段代码:
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Demo4 { /**
* @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.setSubject("Java邮件“);
MimeMultipart mp = new MimeMultipart("mixed");
msg.setContent(mp); MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
MimeBodyPart content = new MimeBodyPart();
mp.addBodyPart(attch1);
mp.addBodyPart(attch2);
mp.addBodyPart(content); DataSource ds1 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif"
);
DataHandler dh1 = new DataHandler(ds1 );
attch1.setDataHandler(dh1);
attch1.setFileName("java.txt");
DataSource ds2 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif"
);
DataHandler dh2 = new DataHandler(ds2 );
attch2.setDataHandler(dh2);
attch2.setFileName("slogo.gif");
MimeMultipart bmp = new MimeMultipart("related");
content.setContent(bmp);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bmp.addBodyPart(htmlPart);
bmp.addBodyPart(gifPart); DataSource gifds = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\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 os = new FileOutputStream("D:\\java\\javamail\\demo4.eml");
msg.writeTo(os);
os.close();
}}第二段代码:
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Demo3 { /**
* @param args
* @throws Exception
*/
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.setSubject("JAVA邮件");
MimeMultipart mp = new MimeMultipart("mixed");
msg.setContent(mp); MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
MimeBodyPart content = new MimeBodyPart();
mp.addBodyPart(attch1);
mp.addBodyPart(attch2);
mp.addBodyPart(content); DataSource ds1 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif");
DataHandler dh1 = new DataHandler(ds1);
attch1.setDataHandler(dh1);
attch1.setFileName("attch1.txt"); DataSource ds2 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif");
DataHandler dh2 = new DataHandler(ds2);
attch2.setDataHandler(dh2);
attch2.setFileName("attch2.txt");
MimeMultipart bmp = new MimeMultipart("related");
content.setContent(bmp);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bmp.addBodyPart(htmlPart);
bmp.addBodyPart(gifPart); DataSource gifds = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\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'>",
"test/html;charset=gbk");
msg.saveChanges();
OutputStream os = new FileOutputStream("D:\\java\\javamail\\demo3.eml");
msg.writeTo(os);
os.close();
}}
而第二段代码生成的内容全在附件里
实在搞不清是怎么回事第一段代码:
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Demo4 { /**
* @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.setSubject("Java邮件“);
MimeMultipart mp = new MimeMultipart("mixed");
msg.setContent(mp); MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
MimeBodyPart content = new MimeBodyPart();
mp.addBodyPart(attch1);
mp.addBodyPart(attch2);
mp.addBodyPart(content); DataSource ds1 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif"
);
DataHandler dh1 = new DataHandler(ds1 );
attch1.setDataHandler(dh1);
attch1.setFileName("java.txt");
DataSource ds2 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif"
);
DataHandler dh2 = new DataHandler(ds2 );
attch2.setDataHandler(dh2);
attch2.setFileName("slogo.gif");
MimeMultipart bmp = new MimeMultipart("related");
content.setContent(bmp);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bmp.addBodyPart(htmlPart);
bmp.addBodyPart(gifPart); DataSource gifds = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\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 os = new FileOutputStream("D:\\java\\javamail\\demo4.eml");
msg.writeTo(os);
os.close();
}}第二段代码:
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Demo3 { /**
* @param args
* @throws Exception
*/
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.setSubject("JAVA邮件");
MimeMultipart mp = new MimeMultipart("mixed");
msg.setContent(mp); MimeBodyPart attch1 = new MimeBodyPart();
MimeBodyPart attch2 = new MimeBodyPart();
MimeBodyPart content = new MimeBodyPart();
mp.addBodyPart(attch1);
mp.addBodyPart(attch2);
mp.addBodyPart(content); DataSource ds1 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif");
DataHandler dh1 = new DataHandler(ds1);
attch1.setDataHandler(dh1);
attch1.setFileName("attch1.txt"); DataSource ds2 = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\slogo.gif");
DataHandler dh2 = new DataHandler(ds2);
attch2.setDataHandler(dh2);
attch2.setFileName("attch2.txt");
MimeMultipart bmp = new MimeMultipart("related");
content.setContent(bmp);
MimeBodyPart htmlPart = new MimeBodyPart();
MimeBodyPart gifPart = new MimeBodyPart();
bmp.addBodyPart(htmlPart);
bmp.addBodyPart(gifPart); DataSource gifds = new FileDataSource(
"D:\\软件\\JAVA\\src\\JavaMail使用的附件\\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'>",
"test/html;charset=gbk");
msg.saveChanges();
OutputStream os = new FileOutputStream("D:\\java\\javamail\\demo3.eml");
msg.writeTo(os);
os.close();
}}
解决方案 »
- request.getParameter()
- 怎样用servlet进行对数据库的操作
- jobss 可以加载什么项目,web醒目 还是EJB项目
- 本人写了个 spring AOP 小例子,结果却在意料之外,不知哪出了问题,还望指教!
- ejb基础问题
- 如何为xml解析器配置一个xsd校验文件,那位高人知道?不胜感激! ^_^
- jboss的各位大哥救命!
- weblogic建立的server,用startWebLogic.cmd启动后要求输入用户名和密码,system/security输入后自动跳出
- 请教一个简单的EJB-QL的语法问题,请多多指教
- myeclipse报错java.sql.SQLException: Access denied for user 'root'@'localhost' (usi
- 为何用Spring?
- javamail发邮件始终报javax.mail.NoSuchProviderException: smtp错误
"test/html;charset=gbk"); 改成
text/html
import java.util.Properties;import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class SendMail {
private static MailAuthenticator autherticator=null;
public static void main(String[] args) {
for (int i=0;i<5;i++){
String from="[email protected]";
String to="[email protected]";
String smtpServer="smtp.163.com";
String subject="Hello ,this is a email Test!!";
String content ="Welcome to you!!";
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
autherticator = new MailAuthenticator("[email protected]","xtsaiyy");
Session session = Session.getDefaultInstance(props,autherticator);
MimeMessage msg = new MimeMessage(session);
try{
msg.setFrom(new InternetAddress(from));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(content);
Transport.send(msg);
System.out.println("成功发送邮件......");
}catch(Exception se){
se.printStackTrace();
}
}
}
}
//现在的大部分的邮件服务器都要求有身份验证,所以需要此类实现验证功能
class MailAuthenticator extends Authenticator{
private String username = null;
private String userpasswd = null;
public MailAuthenticator(){}
public MailAuthenticator(String username,String userpasswd){
this.username = username;
this.userpasswd = userpasswd;
}
public void setUserName(String username){
this.username = username;
}
public void setPassword(String password){
this.userpasswd = password;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,userpasswd);
}
}