文档详情

JavaMail发送邮件实例-给多人发送、抄送.doc

发布:2017-12-15约6.42千字共6页下载文档
文本预览下载声明
JavaMail,顾名思义,提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它可以方便地执行一些常用的邮件传输。我们可以基于JavaMail开发出类似于Microsoft Outlook的应用程序。 虽然JavaMail是Sun的API之一,但它目前还没有被加在标准的java开发工具包中(Java Development Kit),这就意味着你在使用前必须另外下载JavaMail文件。 JavaMail包中用于处理电子邮件的核心类是:Session,Message,Address,Authenticator,Transport,Store,Folder等。Session定义了一个基本的邮件会话,它需要从Properties中读取类似于邮件服务器,用户名和密码等信息。 关键技术: 1、MimeMessage的setRecipients方法设置邮件的收件人,其中Message.RecipientType.TO常量表示收件人类型是邮件接收者,Message.RecipientType.CC常量表示收件人类型是抄送者,Message.RecipientType.BCC常量表示收件人的类型是密送着。 2、在调用MimeMessage的setRecipients方法时,除了可以指定收件人的类型外,还可以传入一个数组,指定多个收件人的地址。 SourceCode: import?javax.mail.Address; import?javax.mail.BodyPart; import?javax.mail.Message; import?javax.mail.MessagingException; import?javax.mail.Multipart; import?javax.mail.Session; import?javax.mail.Transport; import?ernet.InternetAddress; import?ernet.MimeBodyPart; import?ernet.MimeMessage; import?ernet.MimeMultipart; public?class?MultiMailsender?{ ????/** ?????*?发送邮件给多个接收者 ?????*?@param?mailInfo????带发送邮件的信息 ?????*?@return ?????*/ ????public?static?boolean?sendMailtoMultiReceiver(MultiMailSenderInfo?mailInfo){ ????????MyAuthenticator?authenticator?=?null; ????????if?(mailInfo.isValidate())?{ ????????????authenticator?=?new?MyAuthenticator(mailInfo.getUserName(), ????????????????????mailInfo.getPassword()); ????????} ????????Session?sendMailSession?=?Session.getInstance(mailInfo ????????????????.getProperties(),?authenticator); ????????try?{ ????????????Message?mailMessage?=?new?MimeMessage(sendMailSession); ????????????//?创建邮件发送者地址 ????????????Address?from?=?new?InternetAddress(mailInfo.getFromAddress()); ????????????mailMessage.setFrom(from); ????????????//?创建邮件的接收者地址,并设置到邮件消息中 ????????????Address[]?tos?=?null; ????????????String[]?receivers?=?mailInfo.getReceivers(); ????????????if?(receivers?!=?null){ ????????????????//?为每个邮件接收者创建一个地址 ????????????????tos?=?new?InternetAddress[receivers.length?+?1]; ????????????????tos[0]?=?new?InternetAddress(mailInfo.getToAddress()); ??????
显示全部
相似文档