在我们的日常工作中,java的存在变得越来越便利和重要。人们对于它的认知也越来越深刻,更多的人投入到这其中的学习中来。今天主要为大家介绍在java中邮件协议对接的端口主要有哪几种,以及接收html格式的邮件的方法,通过实例来展示给大家。
首先,在实际的应用中,java中的邮件协议对接端口可能会根据需要进行调整。在我们的很多实际应用中,一般很少会需要直接配置邮件服务器,而是直接使用这些邮件服务器。尤其是对于许多免费的服务器来说,针对他们开放的端口和协议我们可以通过使用程序来实现和它们的交互,实现收发邮件。以下是几个典型的邮件协议和对应的端口:
接下来再为大家展示一个接收html格式的邮件操作代码:
package zhang.stony.mail; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Authenticator; 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; /** * @author Stony Zhang (Mybeautiful) * @Createdate 2010-8-12 * @Emal:stonyz@live.com * @QQ:55279427 */ public class MailSender { private MailSetting mSetting; private Session session; public MailSender(MailSetting mSetting) { this.mSetting = mSetting; final String username = mSetting.getUser(); final String password = mSetting.getPassword(); Properties props = new Properties(); props.put("mail.smtp.host", mSetting.getSmtpHost()); props.put("mail.smtp.auth", "true"); session = Session.getInstance(props, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); } public void sentMessage(String titile, String msg, String[] to) throws MessagingException { this.sentMessage(titile, msg, to, null); } public void sentMessage(String titile, String msg, String[] to, String[] cc) throws MessagingException { Message mailMessage = new MimeMessage(session); Address from = new InternetAddress(mSetting.getUser()); mailMessage.setFrom(from); // Address to = new InternetAddress(mSetting.getUser()); Address[] tos = new Address[to.length]; for (int i = 0; i < to.length; i++) { tos[i] = new InternetAddress(to[i]); } mailMessage.setRecipients(Message.RecipientType.TO, tos); if (cc != null && cc.length > 0) { Address[] ccs = new Address[cc.length]; for (int i = 0; i < cc.length; i++) { ccs[i] = new InternetAddress(cc[i]); } mailMessage.setRecipients(Message.RecipientType.CC, ccs); } mailMessage.setSubject(titile); mailMessage.setSentDate(new Date()); // mailMessage.setText(msg); BodyPart mdp = new MimeBodyPart(); mdp.setContent(msg, "text/html;charset=gb2312"); Multipart mm = new MimeMultipart(); mm.addBodyPart(mdp); mailMessage.setContent(mm); mailMessage.saveChanges(); Transport.send(mailMessage); } }
以上就是关于在java中的邮件协议对接端口的分类图以及通过一个实例代码为大家展示,怎么样接收html格式的邮件。如果对这些内容感兴趣的话,或者是有更多java常见问题。敬请关注奇Q工具网。
推荐阅读: