如何在java中附加生成的pdf文件邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18075323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to attach generated pdf file mail in java
提问by AloNE
Some body please tell me about how to attach available pdf file
to mail
.
I am use mail.jar
and activation.jar
for sending a mail
.
I can send mail
but I dont know how to send pdf file
with attachment.
so please suggest me about that
thank you.
一些机构请告诉我如何附加pdf file
到mail
. 我正在使用mail.jar
和activation.jar
发送mail
. 我可以发送,mail
但我不知道如何pdf file
通过附件发送。所以请给我建议,谢谢。
I just try
我只是尝试
String filename = "file.pdf";
Multipart multipart1 = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart1.addBodyPart(messageBodyPart);
But still it not get data, it empty attached.
但它仍然没有获取数据,它是空的。
采纳答案by Ankur Lathi
Send E-Mail with Attachment using JavaMail :
使用 JavaMail 发送带附件的电子邮件:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
class SendAttachment
{
public static void main(String [] args)
{
String to="[email protected]";//change accordingly
final String user="[email protected]";//change accordingly
final String password="xxxxx";//change accordingly
//1) get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", "mail.javatpoint.com");
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password); } });
//2) compose message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Message Aleart");
//3) create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "SendAttachment.java";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//6) set the multiplart object to the message object
message.setContent(multipart );
//7) send message
Transport.send(message);
System.out.println("message sent....");
}catch (MessagingException ex) {ex.printStackTrace();}
}
}
source: http://www.javatpoint.com/example-of-sending-attachment-with-email-using-java-mail-api
来源:http: //www.javatpoint.com/example-of-sending-attachment-with-email-using-java-mail-api
回答by Juned Ahsan
Try this:
尝试这个:
String SMTP_HOST_NAME = "mail.domain.com";
String SMTP_PORT = "111";
String SMTP_FROM_ADDRESS="[email protected]";
String SMTP_TO_ADDRESS="[email protected]";
String subject="Textmsg";
String fileAttachment = "C:\filename.pdf";
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", SMTP_PORT );
Session session = Session.getInstance(props,new javax.mail.Authenticator()
{protected javax.mail.PasswordAuthentication
getPasswordAuthentication()
{return new javax.mail.PasswordAuthentication("[email protected]","password");}});
try{
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(SMTP_FROM_ADDRESS));
// create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart();
//fill message
messageBodyPart.setText("Test mail one");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
// Put parts in message
msg.setContent(multipart);
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SMTP_TO_ADDRESS));
msg.setSubject(subject);
// msg.setContent(content, "text/plain");
Transport.send(msg);
System.out.println("success....................................");
}
catch(Exception e){
e.printStackTrace();
}
Source: http://www.coderanch.com/t/586537/java/java/sample-code-java-mail-api
来源:http: //www.coderanch.com/t/586537/java/java/sample-code-java-mail-api
回答by Azodious
You should create MimeMultipart
, MimeBodyPart
, FileDataSource
and DataHandler
as follows:
您应该创建MimeMultipart
,MimeBodyPart
,FileDataSource
和DataHandler
如下:
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String filePath = "<file system path>";
String fileName = "display name"
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
Now, On MimeMessage
use setContent
method.
现在,MimeMessage
使用setContent
方法。