以下文章提供了用 Java 发送电子邮件附件的概述。将电子邮件与电子邮件服务提供商的凭据连接起来的功能是启用发送电子邮件附件的功能。要完成此操作,必须使用电子邮件主机服务,然后输入电子邮件主机、端口、用户名和密码来创建 Session 对象。电子邮件主机服务还额外提供所有这些规范和代码,可以利用任何虚假或其他 SMTP 测试服务器,并管理 JavaMail 的配置和身份验证,会话对象将充当连接工厂。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
JavaMail API 提供了各种有用的类,例如 BodyPart 和 MimeBodyPart,用于发送带有附件的电子邮件。要了解 JavaMail API 的电子邮件发送阶段并理解它,应加载以下两个 jar 文件以便使用 JavaMail API 发送电子邮件:
既然我们已经有了 Session 对象,那么让我们构建 MimeMessage 和 MimeBodyPart 对象。
为了生成电子邮件消息,我们使用以下对象:
另外,Java电子邮件程序中的步骤如下:
必须配置电子邮件服务提供商的凭据。然后,输入电子邮件主机、端口、用户名和密码来构建会话对象。电子邮件主机服务提供所有这些细节。对于代码,我们可以利用任何虚假的 SMTP 测试服务器。为了管理 JavaMail 的配置和身份验证,会话对象将充当连接工厂。现在我们已经有了 Session 对象,可以构建 MimeMessage 和 MimeBodyPart 对象。为了生成电子邮件消息,我们使用以下对象:
代码:
Message testmsg = new MimeMessage(sess); testmsg.setFrom(new InternetAddress(fromaddr)); testmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); testmsg.setSubject("Welcome to our Test Site");
上面的代码有助于在 MimeMessage 会话对象的帮助下创建消息服务。这样我们就可以将会话(sess)作为消息队列的参数传递。在实例的帮助下,我们可以调用名为 setFrom() 的方法,用于传递发件人地址或发件人列表。在同一个实例中,我们可以调用另一个名为 setRecipients() 的方法,它具有像 Message 类一样的参数传递,并具有一个名为 RecipientType 的附加方法,用于调用 To 邮件接收者,此外还可以调用 InternetAddress 以及名为 parse(to) 的默认方法,用于传递目标地址收件人。 setSubject() 在 Message 类实例中传递字符串值。上面的摘录中已生成 MimeMessage 对象,其中包含必要的信息,包括发件人、收件人和主题。接下来,我们有一个包含电子邮件正文的 MimeBodyPart 对象。此外,要向邮件服务添加附件,我们现在应该构建另一个 MimeBodyPart。
代码:
MimeBodyPart mpart = new MimeBodyPart(); mpart.attachFile(new File("path/to/file"));
The MimeBodyPart is the mail class body that created the instance and the same will be called for the method called attachFile() with additional parameters like to pass the new File object creation with file path parameters. TLS authentication is possible but different in several ways. As we can see, we are calling additional EmailUtil class methods to deliver email attachments and images even though we haven’t yet defined them.
Given below is the example mentioned:
Code:
package TestNG; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class NewTest{ public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); props.put("mail.debug", "true"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory"); Session sess = Session.<em><i>getDefaultInstance</i></em>(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("[email protected]","xodbizaoiqijifre"); } }); try{ MimeMessage msg = new MimeMessage(sess); msg.setFrom(new InternetAddress("[email protected]")); msg.addRecipient(Message.RecipientType.<em><i>TO</i></em>,new InternetAddress("[email protected]")); msg.setSubject("Welcome To My Domain"); BodyPart mbody = new MimeBodyPart(); mbody.setText("Your Message body is sent"); MimeBodyPart mbody1 = new MimeBodyPart(); String filename = "D://articles1.txt"; DataSource source = new FileDataSource(filename); mbody1.setDataHandler(new DataHandler(source)); mbody1.setFileName(filename); Multipart mpart = new MimeMultipart(); mpart.addBodyPart(mbody); mpart.addBodyPart(mbody1); msg.setContent(mpart ); Transport.<em><i>send</i></em>(msg); System.<em><i>out</i></em>.println("Your email is sent successfully"); }catch (MessagingException ex) {ex.printStackTrace();} } }
Output:
Given below are the FAQs mentioned:
Answer: When enabling the functionality of sending email attachments, the email host, port, username, and password are entered after configuring the email with the email service provider’s credentials.
Answer: The abstract Multipart class is implemented by the MimeMultipart class, which employs MIME standards for multipart data.
Answer: Get the compose message for the session object.
Create a MimeBodyPart object and specify the message text in it. Create another MimeBodyPart object and add a DataHandler object to it. Create a Multipart object and include MimeBodyPart objects in it.
Send a message by setting the multipart object to the message object.
To obtain the session object, which contains all of the host’s data, including hostname, username, and password. Write the message, the message along with the attachment, and send it. The JavaMail API provides a platform- and protocol-neutral foundation for building mail and messaging applications. The JavaMail API makes a number of abstract classes defining the components of a mail system available.
以上是用Java发送电子邮件附件的详细内容。更多信息请关注PHP中文网其他相关文章!