标题说明了所有内容: 如何将pdf文件提交给来自Java应用程序的通用电子邮件?如何通过来自Java的电子邮件发送pdf
回答
可以使用这个参考用PDF文件发送电子邮件作为附件 -
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
class SendMailWithAttachment
{
public static void main(String [] args)
{
String to="[email protected]"; //Email address of the recipient
final String user="[email protected]"; //Email address of sender
final String password="xxxxx"; //Password of the sender's email
//Get the session object
Properties properties = System.getProperties();
//Here pass your smtp server url
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); } });
//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");
//Create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//Create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = "YourPDFFileName.pdf";//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
//Create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
//Set the multiplart object to the message object
message.setContent(multipart);
//Send message
Transport.send(message);
System.out.println("message sent....");
}catch (MessagingException ex) {ex.printStackTrace();}
}
}
您也可以参考JavaTPoint
谢谢。但我收到此错误: com.sun.mail.smtp.SMTPSendFailedException:550访问被拒绝 - 无效HELO域名(参见RFC2821 4.1.1.1) \t在com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport。的java:1829) \t在com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368) \t在com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886) \t在的javax .mail.Transport.send0(Transport.java:191) \t在javax.mail.Transport.send(Transport.java:120) \t在testemail.SendMailWithAttachment.main(SendMailWithAttachment.java:53) –
@Da尼尔桑我认为你只是复制上面的代码。我在回答中添加了评论,请检查一下。 –
- 1. 如何通过电子邮件发送电子邮件地址
- 2. 无法通过CakePHP邮件发送电子邮件附件PDF
- 3. 如何通过电子邮件从JaserReports/iReport发送报告pdf
- 4. 如何通过电子邮件发送PDF而不打开它
- 5. Pimcore如何创建PDF并通过电子邮件发送
- 6. 通过XForm发送来自Episerver的电子邮件
- 7. 如何通过自动发送的电子邮件中的NetSuite
- 8. java如何通过任何帐户发送电子邮件
- 9. Rails 4 - 恶毒的pdf文件通过电子邮件发送
- 10. Java来通过Gmail发送电子邮件
- 11. 通过带有java邮件API的struts发送电子邮件
- 12. 发送来自django的电子邮件
- 13. 发送来自mIRC的电子邮件
- 14. 通过Gmail发送电子邮件来自不同帐户
- 15. 通过电子邮件发送的navigator.userAgent
- 16. 通过电子邮件发送文件
- 17. 如何通过Java从Outlook发送电子邮件?
- 18. 通过电子邮件发送Google Doc作为PDF附件
- 19. 使用FPDF通过PHP发送电子邮件PDF附件
- 20. Java邮件:通过gmail与TLS发送电子邮件
- 21. 通过Java邮件发送电子邮件
- 22. Excel自动通过Outlook发送电子邮件随机发送
- 23. 通过PHP向动态创建的PDF发送电子邮件
- 24. 通过电子邮件发送可填写的PDF表格
- 25. 通过电子邮件发送PDF格式在Rails的
- 26. 如何使用php curl发送来自yahoo电子邮件的电子邮件
- 27. ASP.NET通过电子邮件发送给多个电子邮件
- 28. GAS帮助 - 通过电子邮件发送电子邮件
- 29. 如何设置Jaspersoft的iRreport发送自动.pdf电子邮件?
- 30. 通过Visual C++/Cli不发送邮件发送电子邮件
请编辑您的问题,除去PDFBox的所有提及。你的问题基本上是如何通过使用java的smtp发送文件。 –