首页  >  文章  >  Java  >  Java电子邮件

Java电子邮件

王林
王林原创
2024-08-30 16:21:59791浏览

通过java进程发送电子邮件是一个简单且容易实现的过程。该过程是一个即时过程,基于java的电子邮件过程的两个必备项是JavamailAPI和JAF框架。这是用 java 发送电子邮件的两个主要部分。这些部分使基于 Java 的应用程序中的电子邮件发送过程变得更加简单。 Java邮件API和JAF都可以从java标准网站下载。 smtp 服务器也可用于发送电子邮件。此 SMTP 服务器用法是电子邮件生成的替代方法。安装和使用 SMTP 服务器(例如播客服务器和 apache James 服务器)是另一种方法。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

Java 发送电子邮件的步骤

发送邮件的关键步骤如下,

1) 检索会话对象。
2) 撰写要发送的消息。
3) 发送消息。

让我们详细讨论每个步骤,检索会话对象的第一步负责拉取基于会话的对象。创建的每个会话都可能有一个与其关联的对象。这些对象将与会话相关的信息紧密耦合。为了检索与会话相对应的对象,javax.需要使用mail.Session类。此类有两种不同的方法用于检索对象实例详细信息。因此,有两个内置方法用于检索对象实例详细信息:Session。 getdefaultinstance() 方法和 Session. getinstance() 方法。这是提取关联对象详细信息的两个关键方法。要检索会话对象本身,可以使用以下任何方法来处理这种情况,

s.no Method details Description
1 public static Session getDefaultInstance(Properties p) default session value will be returned
2 public static Session getDefaultInstance(Properties p,Authenticator a) default session value will be returned
3 public static Session getInstance(Properties prop) Value associated to the new session will be returned
4 public static Session getInstance(Properties prop,Authenticator a) Value associated to the new session will be returned

撰写消息:这是此过程中需要考虑的非常关键的步骤。该步骤涉及从源头制定原始预期消息的过程。因此,由于本节涉及原始消息,因此这是需要考虑的非常关键的部分。为了发生堆肥过程,使用了 javax.mail.message。此类允许复杂地构建消息。该类处于操作的抽象级别,因此其子类称为 javax.mail.internet.MimeMessage 更专门用于此过程。会话和关联的消息将使用以下代码段进行堆积。因此,此代码用于组合消息和会话详细信息 MimeMessage message=new MimeMessage(session);
发送消息:本节的最后一个过程是发送消息。 javax.邮件。传输类就是用于此目的。该类的目的是触发发送消息的过程。所以具体来说,发送消息的过程可以通过javax.具体是mail.transport消息。从编码的角度来看,Transport 的代码片段。发送(消息);正是用于此过程。

No. Method Description
1 public static void send(Message m) The given method is used for sending the message. So transport of the message can be achieved by means of this method.
2 public static void send(Message m, Address[] address) For sending the message to one specific address this method is used.

Javax 邮件程序步骤

代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class Initiateemail {
public static void main(String [] args) {
// email id of the recipient has to be mentioned in this field
String to = "[email protected]";
// email id of the sender needs to be mentioned here
String from = "[email protected]";
// Assuming you are sending email from localhost
String host = "localhost";
// All details associated to the property are mentioned here
Properties prop = System.getProperties();
// this is the step were the property setup can be eastablished
prop.setProperty("mail.smtp.host", host);
Session ses = Session.getDefaultInstance(prop);
try {
// onject associated to the message is initiated here
MimeMessage mess = new MimeMessage(ses);
// header details are decided and set here.
mes.setFrom(new InternetAddress(from));
// header field details are created here
mes.addRecipient(Mes.RecipientType.TO, new InternetAddress(to));
// subject details of the message are given here
mes.setSubject("Hello world message . . . . . . . . . . . subject starts ");
// Actual message of the email is given here
message.setText("Hello world . . . . . . . . . . . . . . . Message Ends");
// transport object is used for initiating the message
Transport.send(mes);
System.out.println("message has been sent successfully . . . . . ");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

输出:

Java电子邮件

Java电子邮件

JavaMail 在电子邮件中发送附件

代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail {
public static void main(String [] args) {
String to = "[email protected]";
String from = "[email protected]";
String host = "localhost";
Properties prop = System.getProperties();
prop.setProperty("mail.smtp.host", host);
Session ses = Session.getDefaultInstance(prop);
try {
MimeMessage mess = new MimeMessage(ses);
mes.setFrom(new InternetAddress(from));
mes.addRecipient(Mes.RecipientType.TO, new InternetAddress(to));
mes.setSubject("Hello world message . . . . . . . . . . . subject starts ");
message.setText("Hello world . . . . . . . . . . . . . . . Message Ends");
mp.addBodyPart(mbp);
mbp = new MimeBodyPart();
String fl = "newfile.txt";
DataSource src= new FileDataSource(fl);
mbp.setDataHandler(new DataHandler(src));
mbp.setFileName(filename);
mp.addBodyPart(mbp);
messetContent(mp );
Transport.send(mes);
System.out.println("message has been sent successfully . . . . . ");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}

输出:

Java电子邮件

Java电子邮件

结论

本文解释了如何设置 java 电子邮件消息以及设置电子邮件消息涉及哪些类,还描述了用于发送电子邮件以及发送附有文件的电子邮件的程序。

以上是Java电子邮件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Java Email Validation下一篇:EJB in Java