Java电子邮件
通过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(); } } }
输出:
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电子邮件的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

testthepdfinanotherapptoderineiftheissueiswiththefileoredge.2.enablethebuilt inpdfviewerbyTurningOff“ eflblyopenpenpenpenpenpdffilesexternally”和“ downloadpdffiles” inedgesettings.3.clearbrowsingdatainclorwearbrowsingdataincludingcookiesandcachedcachedfileresteroresoreloresorelorsolesoresolesoresolvereresoreorsolvereresoreolversorelesoresolvererverenn

容器化Java应用:创建Dockerfile,使用基础镜像如eclipse-temurin:17-jre-alpine,复制JAR文件并定义启动命令,通过dockerbuild构建镜像并用dockerrun测试本地运行。2.推送镜像到容器注册表:使用dockertag标记镜像并推送到DockerHub等注册表,需先登录dockerlogin。3.部署到Kubernetes:编写deployment.yaml定义Deployment,设置副本数、容器镜像和资源限制,编写service.yaml创建

Importjava.ioandjava.net.SocketforI/Oandsocketcommunication.2.CreateaSocketobjecttoconnecttotheserverusinghostnameandport.3.UsePrintWritertosenddataviaoutputstreamandBufferedReadertoreadserverresponsesfrominputstream.4.Usetry-with-resourcestoautomati

VSCode中可通过快捷键快速切换面板与编辑区。要跳转至左侧资源管理器面板,使用Ctrl Shift E(Windows/Linux)或Cmd Shift E(Mac);返回编辑区可用Ctrl `或Esc或Ctrl 1~9。相比鼠标操作,键盘快捷键更高效且不打断编码节奏。其他技巧包括:Ctrl KCtrl E聚焦搜索框,F2重命名文件,Delete删除文件,Enter打开文件,方向键展开/收起文件夹。

runthewindowsupdatetrubloubleshooterviaSettings>更新&安全> is esseShootsoAtomationfixCommonissues.2.ResetWindowSupDateComponentsByStoppingRealatedServices,RenamingTheSoftWaredWaredWaredSoftwaredSistribution andCatroot2Folders,intrestrestartingthertingthertingtherserviceSteStoceTocle

AwhileloopinJavarepeatedlyexecutescodeaslongastheconditionistrue;2.Initializeacontrolvariablebeforetheloop;3.Definetheloopconditionusingabooleanexpression;4.Updatethecontrolvariableinsidethelooptopreventinfinitelooping;5.Useexampleslikeprintingnumber

要有效使用Mockito进行Java单元测试,首先需添加Mockito依赖,Maven项目在pom.xml中加入mockito-core依赖,Gradle项目添加testImplementation'org.mockito:mockito-core:5.7.0';接着通过@Mock注解(配合@ExtendWith(MockitoExtension.class))或mock()方法创建模拟对象;然后使用when(...).thenReturn(...)等方式对模拟对象的方法行为进行存根,也可配置异

JavaserializationConvertSanObject'SstateIntoAbyTeSteAmForStorageorTransermission,andDeserializationReconstructstheObjectStheObjectFromThstream.1.toenableserialization,aclassMustimustimplementTheSerializableizableface.2.UseObjectObjectObjectObjectOutputputputputputtreamToserialializeanobectizeanobectementeabectenobexpent,savin
