通过GMailSender发送带有附件的电子邮件? [英] Sending email with attachment through GMailSender?
问题描述
我一直在阅读博客,并已试过无数的实现但仍然未能获得附加到我通过Gmail使用Java发送电子邮件的图像。我下载了所有的罐子,并添加GMailSender.java,GMailAuthenticator.java和JSSEProvider.java,我能够定期发送电子邮件就好了。我试着做这件事的方法如下所示,与多数民众赞成注释掉的是我曾希望加入的图像部分的中间部分。下面是当我尝试执行此在的logcat输出。当然,我失去了一些东西pretty的简单。可能有人指出来我好吗?先谢谢了。
市民同步无效的sendmail(字符串学科,身体的字符串,字符串发件人,收件人字符串)抛出异常{
尝试 {
Thread.currentThread()setContextClassLoader(的getClass()getClassLoader()。)。
的MimeMessage消息=新的MimeMessage(会议);
DataHandler的处理程序=新的DataHandler(新ByteArrayDataSource(body.getBytes(),text / plain的));
message.setSender(新网际(发件人));
message.setSubject(学科);
message.setDataHandler(处理);
/ *
//创建新的邮件部分
BodyPart的imgPart =新MimeBodyPart();
//创建一个相关的多部分的零件组合
MimeMultipart的多部分=新MimeMultipart的(相关);
multipart.addBodyPart(imgPart);
字符串文件名=HTTP://.../sampleBar$c$c.png;
类加载器的类加载器= Thread.currentThread()getContextClassLoader()。
如果(类加载器== NULL){
类加载器= this.getClass()getClassLoader()。
如果(类加载器== NULL){
的System.out.println(IT IS NULL AGAIN !!!!);
}
}
数据源DS =新一个URLDataSource(ClassLoader.getResource方法,(文件名));
imgPart.setDataHandler(新的DataHandler(DS));
imgPart.setHeader(内容ID,&其中; logoimg_cid>中);
multipart.addBodyPart(imgPart);
message.setContent(多部分);
* /
如果(recipients.indexOf(,)大于0){
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(收件人));
}
其他message.setRecipient(Message.RecipientType.TO,新的网际(收件人));
Transport.send(消息);
}
赶上(例外五){Log.e(EMAIL_ERROR,e.getMessage(),E);}
}
空值
显示java.lang.NullPointerException
在javax.activation.URLDataSource.getContentType(URLDataSource.java:91)
在javax.activation.DataHandler.getContentType(DataHandler.java:218)
...
...
(加上一些更多)
我修改了函数接受文件的参数,并将其附加到电子邮件,这里是
市民同步无效的sendmail(字符串学科,身体的字符串,字符串发件人,收件人的字符串,文件附件)抛出异常{
尝试{
的MimeMessage消息=新的MimeMessage(会议);
message.setSender(新网际(发件人));
message.setSubject(学科);
MimeBodyPart MBP1 =新MimeBodyPart();
mbp1.setText(体);
MimeBodyPart mbp2 =新MimeBodyPart();
FileDataSource FDS =新FileDataSource(挂职);
mbp2.setDataHandler(新的DataHandler(FDS));
mbp2.setFileName(fds.getName());
多部分MP =新MimeMultipart的();
mp.addBodyPart(MBP1);
mp.addBodyPart(mbp2);
message.setContent(MP);
如果(recipients.indexOf(,)大于0)
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(收件人));
其他
message.setRecipient(Message.RecipientType.TO,新的网际(收件人));
Transport.send(消息);
}赶上(例外五){
}
}
I have been reading the blogs and have tried numerous implementations but have still failed to get an image attached to an email that I'm sending through GMail using java. I downloaded all the jars and added GMailSender.java, GMailAuthenticator.java, and JSSEProvider.java and I'm able to send regular e-mails just fine. The way I've tried doing it is shown below, with the middle part that's commented out being the part I had hoped would add the image. Below that is the output on the logcat when I try to execute this. Surely I am missing something pretty simple. Could someone point it out to me please? Thanks in advance.
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setDataHandler(handler);
/*
// Create your new message part
BodyPart imgPart = new MimeBodyPart();
// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(imgPart);
String fileName = "http://.../sampleBarcode.png";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = this.getClass().getClassLoader();
if (classLoader == null) {
System.out.println("IT IS NULL AGAIN!!!!");
}
}
DataSource ds = new URLDataSource(classLoader.getResource(fileName));
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader("Content-ID", "<logoimg_cid>");
multipart.addBodyPart(imgPart);
message.setContent(multipart);
*/
if(recipients.indexOf(',') > 0) {
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
}
else message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
catch(Exception e){Log.e("EMAIL_ERROR",e.getMessage(), e);}
}
null
java.lang.NullPointerException
at javax.activation.URLDataSource.getContentType(URLDataSource.java:91)
at javax.activation.DataHandler.getContentType(DataHandler.java:218)
...
...
(plus some more)
I modified the function to accept a File parameter and attach it to the email, here it is
public synchronized void sendMail(String subject, String body, String sender, String recipients, File attachment) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(body);
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attachment);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
message.setContent(mp);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
这篇关于通过GMailSender发送带有附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!