在java中作为附件发送一个文件 [英] Send a file as an attachment in java
问题描述
我有一个java代码,我必须发送附件,它可能是 .doc,.db或.file 。所以我使用下面的代码,邮件已成功传递,特定的附件文件不发送,也收到。
我的代码是:
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
导入javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmail {
private String from =example1@gmail.com;
私人字符串;
private String subject;
私人字符串文本;
String filename =hardWare_Dtls.file;
String host =smtp.gmail.com;
$ b $ public SendEmail(String from,String to,String subject,String text,String filename){
// System.out.println(From Adress inside constr+ from);
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
this.filename = filename;
$ b $ public void send(){
属性props = System.getProperties();
System.out.println(电子邮件选项SendEmail);
props.put(mail.smtp.host,host);
props.put(mail.smtp.user,from);
props.put(mail.smtp.password,123456);
props.put(mail.smtp.port,587); // 587是yahoo
的端口号// mail
props.put(mail.smtp.auth,true);
props.put(mail.smtp.starttls.enable,true);
会话mailSession = Session.getDefaultInstance(props,null);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
尝试{
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
$ b $ catch(AddressException e){
// TODO自动生成的catch块
// e.printStackTrace();
System.out.println(Address Exception+ e.getMessage());
}
尝试{
System.out.println(From Address+ fromAddress);
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO,toAddress);
System.out.println(To Address+ toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
simpleMessage.setSentDate(new Date());
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename){
@Override
public String getContentType(){
returnapplication / octet-stream;
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
// multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
// simpleMessage.setText(attachment);
Transport transport = mailSession.getTransport(smtps);
transport.connect(smtp.gmail.com,example1@gmail.com,
123456);
simpleMessage.saveChanges();
transport.sendMessage(simpleMessage,
simpleMessage.getAllRecipients());
Transport.send(simpleMessage);
transport.close();
// Transport.send(simpleMessage);
} catch(MessagingException e){
// TODO自动生成的catch块
// e.printStackTrace();
System.out.println(Messagine Exception+ e.getMessage());
$ b code
$ b $ p
$ b $ p 编码我有什么错误。如果使用Javacode发送附件的消息的其他代码,请寄给我。感谢提前。
我在我的主要类中使用此代码:
来自=example@gmail.com的字符串;
String to =example@yahoo.com;
String subject =Sample Text Message;
String消息=带有文件附件的示例消息;
String filename =hardWare_Dtls.file;
SendEmail sendMail = new SendEmail(from,to,subject,message,filename);
sendMail.send();
解决方案一个 MimeMessage (而不只是消息),并将其内容设置为您的Multipart。我认为你真的试过这样做,但你的代码不会编译,所以你注释掉了这一行:
// simpleMessage。的setText(附着);
而不是这样做:
<$ p $(MimeMessage)simpleMessage).setContent(multipart);
再次测试。它应该工作。
i have a java code in that i must send a attachment, it may be .doc,.db or .file. So i use the following code, the message was delivered successfully and particular attachment file was not send and also received.
My Code is:
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendEmail {
private String from = "example1@gmail.com";
private String to;
private String subject;
private String text;
String filename = "hardWare_Dtls.file";
String host = "smtp.gmail.com";
public SendEmail(String from, String to, String subject, String text,String filename) {
// System.out.println("From Adress inside constr"+from);
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
this.filename=filename;
}
public void send() {
Properties props = System.getProperties();
System.out.println("Email Options SendEmail");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "123456");
props.put("mail.smtp.port", "587"); // 587 is the port number of yahoo
// mail
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session mailSession = Session.getDefaultInstance(props, null);
Message simpleMessage = new MimeMessage(mailSession);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("Address Exception" + e.getMessage());
}
try {
System.out.println("From Address" + fromAddress);
simpleMessage.setFrom(fromAddress);
simpleMessage.setRecipient(RecipientType.TO, toAddress);
System.out.println("To Address" + toAddress);
simpleMessage.setSubject(subject);
simpleMessage.setText(text);
simpleMessage.setSentDate(new Date());
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
// multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
// simpleMessage.setText(attachment);
Transport transport = mailSession.getTransport("smtps");
transport.connect("smtp.gmail.com", "example1@gmail.com",
"123456");
simpleMessage.saveChanges();
transport.sendMessage(simpleMessage,
simpleMessage.getAllRecipients());
Transport.send(simpleMessage);
transport.close();
// Transport.send(simpleMessage);
} catch (MessagingException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println("Messagine Exception" + e.getMessage());
}
}
}
in this code what mistake i had. If any other code for sending a message with attachment using Javacode, kindly send me. thanks in Advance.
I use this code in my Main Class:
String from = "example@gmail.com";
String to = "example@yahoo.com";
String subject = "Sample Text Message";
String message = "Sample Msg with File attachment";
String filename="hardWare_Dtls.file";
SendEmail sendMail = new SendEmail(from, to, subject, message,filename);
sendMail.send();
You have pretty much all the pieces but you should use a MimeMessage (instead of just Message), and set its content to be your Multipart. I think you actually tried to do that but your code wouldn't compile so you commented out the line:
// simpleMessage.setText(attachment);
Instead of that do this:
((MimeMessage) simpleMessage).setContent(multipart);
And test again. It should work.
这篇关于在java中作为附件发送一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!