随着时代的不断变化发展,人们对于新技术的渴求越来越强烈,希望能够学习更多的知识,充实自身的同时,也能为自己带来更好的生活。今天就来为大家主要介绍下在java中怎么用163邮箱发送邮件,以及具体的步骤包括哪些。
接下来为大家展示用163邮箱发送邮件的具体步骤:
首先是:授权码+邮箱账号
1.添加maven依赖:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
2.测试邮件发送:
import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * * java实现邮箱发送邮件功能 * * * @author WQ * */ public class JavaMail { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "smtp.163.com"); // smtp服务器地址 Session session = Session.getInstance(props); session.setDebug(true); Message msg = new MimeMessage(session); msg.setSubject("这是一个测试程序...."); msg.setText("你好!这是我的第一个javamail程序---WQ"); msg.setFrom(new InternetAddress("183****1265@163.com")); //发件人邮箱(我的163邮箱) msg.setRecipient(Message.RecipientType.TO , new InternetAddress("183****1265@163.com")); //收件人邮箱(我的QQ邮箱) msg.saveChanges(); Transport transport = session.getTransport(); transport.connect("183****1265@163.com", "d****56"); //发件人邮箱,授权码(可以在邮箱设置中获取到授权码的信息) transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("邮件发送成功..."); transport.close(); } }
下面是163邮箱授权码位置:
接收邮件:
然后是:邮箱账号+邮箱密码
1.添加maven依赖:
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> b、 发送邮件测试
import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.Properties; public class SendEmail { public static String myEmailAccount = "****"; //邮箱账号 public static String myEmailPassword = "****"; //邮箱密码 public static String myEmailSMTPHost = "smtp.ym.163.com"; //发送邮箱服务器地址(这个地址是网易企业邮箱的地址) public static void sendEmail(String text, String receiveEmail) throws Exception { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", myEmailSMTPHost); props.setProperty("mail.smtp.auth", "true"); final String smtpPort = "994"; props.setProperty("mail.smtp.port", smtpPort); props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.setProperty("mail.smtp.socketFactory.fallback", "false"); props.setProperty("mail.smtp.socketFactory.port", smtpPort); Session session = Session.getDefaultInstance(props); session.setDebug(true); // 设置为debug模式, 可以查看详细的发送 log MimeMessage message = createMimeMessage(session, myEmailAccount, receiveEmail, text); Transport transport = session.getTransport(); transport.connect(myEmailAccount, myEmailPassword); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail, String text) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(sendMail, "*****", "UTF-8")); message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "", "UTF-8")); message.setSubject("邮箱验证", "UTF-8"); message.setContent(text, "text/html;charset=UTF-8"); message.setSentDate(new Date()); message.saveChanges(); return message; } public static void main(String[] args) throws Exception { sendEmail("这是内容,验证码45845", "183****1265@163.com"); } }
接收邮件:
接下来为大家简述下,连接到邮件服务器并发送邮件的过程。第一步:连接邮件服务器,身份验证;
第二步: 选择或者设置收件人,主题和邮件内容等;第三步: 选择发送邮件,邮件服务器将内容转发。
以上就是有关如何在java中用163邮箱发送邮件的方式,以及具体的步骤描述。想要了解更多java常见问题,敬请关注奇Q工具网。
推荐阅读: