jmail 如何发送附件?回答给200分

wiki_004 2005-07-27 11:21:16
jmail 如何发送附件?
...全文
106 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
masse 2005-07-27
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4155/4155680.xml?temp=.1220056
joshua_fh 2005-07-27
  • 打赏
  • 举报
回复
public class sendfile {

public static void main(String[] args) {
if (args.length != 5) {
System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
System.exit(1);
}

String to = args[0];
String from = args[1];
String host = args[2];
String filename = args[3];
boolean debug = Boolean.valueOf(args[4]).booleanValue();
String msgText1 = "Sending a file.\n";
String subject = "Sending a file";

// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);

Session session = Session.getInstance(props, null);
session.setDebug(debug);

try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);

// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);

// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();

// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());

// create the Multipart and add its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);

// add the Multipart to the message
msg.setContent(mp);

// set the Date: header
msg.setSentDate(new Date());

// send the message
Transport.send(msg);

} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
}
masse 2005-07-27
  • 打赏
  • 举报
回复
---------------TEST-----------------------------
public static void main(String[] args)
{
try
{
EmailSender sender = new EmailSender("smtp.163.net", "username", "password");

sender.setMailFrom("xxx1@163.net");
sender.setMailTo("xxx2@s163.com");
sender.setSubject("测试 java mail");
sender.setContent("测试the content of the mail");
sender.setAttachFile("D:\\test.txt");
System.out.println("Sending email .....");
sender.sendMultiPartMail();
System.out.println("success!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
---------------------------------------------------------------------------
masse 2005-07-27
  • 打赏
  • 举报
回复
用javamail收发附件的例子
------------------------EmailAuthenticator.java---------------------
package com.xxx.helper.javamail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


public class EmailAuthenticator extends javax.mail.Authenticator
{
private String userName = null;
private String password = null;

public void setUserName(String aUserName)
{
this.userName = aUserName;
}

public void setPassword(String aPassword)
{
this.password = aPassword;
}

public EmailAuthenticator()
{
super();
}

public EmailAuthenticator(String aUserName, String aPassword)
{
super();
setUserName(aUserName);
setPassword(aPassword);
}

//一定要有这个方法,它是在需要身份验证时自动被调用的
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.userName, this.password);
}
}
------------------------------------EmailSender.java-----------------------------------
package com.apsoft.helper.javamail;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.SendFailedException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Date;
import java.util.Properties;
import java.io.File;
import java.io.UnsupportedEncodingException;


public class EmailSender
{
private Session session = null;

private String mailFrom = null;
private String mailTo = null;
private String mailCc = null;
private String mailBcc = null;

private String subject = null;
private String content = null;

private String attachFile = null;

private String mailHost = null;
private String userName = null;
private String password = null;


public EmailSender(String mailHost)
{
Properties prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", mailHost);

this.session = Session.getDefaultInstance(prop, null);
}

public EmailSender(String mailHost, String userName, String password)
{
Properties prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", mailHost);
prop.put("mail.smtp.auth", "true");

this.session = Session.getInstance(prop,
new EmailAuthenticator(userName, password));
// this.session.setDebug(true);
}

public EmailSender(String mailHost, String proxyHost, int proxyPort)
{
Properties prop = new Properties();
prop.put("http.proxySet", "true" );
prop.put("http.proxyHost", proxyHost);
prop.put("http.proxyPort", String.valueOf(proxyPort));
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", mailHost);


this.session = Session.getDefaultInstance(prop, null);
}

public EmailSender(String mailHost, String userName, String password,
String proxyHost, int proxyPort)
{
Properties prop = new Properties();
prop.put("http.proxySet", "true" );
prop.put("http.proxyHost", proxyHost);
prop.put("http.proxyPort", String.valueOf(proxyPort));
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", mailHost);
prop.put("mail.smtp.auth", "true");

this.session = Session.getDefaultInstance(prop,
new EmailAuthenticator(userName, password));
}


public void sendSimpleMail() throws MessagingException,SendFailedException
{
if(this.mailFrom == null)
{
throw new RuntimeException("NO MAIL FROM");
}
if(this.mailTo == null)
{
throw new RuntimeException("NO MAIL TO");
}

MimeMessage mimeMsg = new MimeMessage(this.session);

mimeMsg.setFrom(new InternetAddress(mailFrom) );
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo) );

if(mailCc != null)
{
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mailCc) );
}

if(mailBcc != null)
{
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailBcc) );
}

mimeMsg.setSubject(subject,"GBK");
mimeMsg.setText(content ,"GBK" );
mimeMsg.setSentDate(new Date());

Transport.send(mimeMsg);
}
//带附件
public void sendMultiPartMail()
throws MessagingException,SendFailedException,UnsupportedEncodingException
{
if(this.mailFrom == null)
{
throw new RuntimeException("NO MAIL FROM");
}
if(this.mailTo == null)
{
throw new RuntimeException("NO MAIL TO");
}
if(this.attachFile == null)
{
throw new RuntimeException("NO MAIL TO");
}
File file = new File(attachFile);
if(!file.canRead())
{
throw new RuntimeException("Can't find the attached file!");
}

MimeMessage mimeMsg = new MimeMessage(this.session);

mimeMsg.setFrom(new InternetAddress(mailFrom) );
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo) );

if(mailCc != null)
{
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mailCc) );
}

if(mailBcc != null)
{
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailBcc) );
}

mimeMsg.setSubject(subject,"GBK");

MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(content, "GBK");

MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(this.attachFile);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(MimeUtility.encodeWord(fds.getName()));

MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);

mimeMsg.setContent(mp);

mimeMsg.setSentDate(new Date());

Transport.send(mimeMsg);

}

public void setMailFrom(String mailFrom)
{
this.mailFrom = mailFrom;
}

public void setMailTo(String mailTo)
{
this.mailTo = mailTo;
}

public void setMailCc(String mailCc)
{
this.mailCc = mailCc;
}

public void setMailBcc(String mailBcc)
{
this.mailBcc = mailBcc;
}

public void setSubject(String subject)
{
this.subject = subject;
}

public void setContent(String content)
{
this.content = content;
}

public void setAttachFile(String attachFile)
{
this.attachFile = attachFile;
}
}
-------------------------------------------------------------------------

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧