「java邮件传输」javamail发送邮件

博主:adminadmin 2023-01-12 06:27:07 698

本篇文章给大家谈谈java邮件传输,以及javamail发送邮件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何使用Java发送qq邮件

方法:

1.前提准备工作:

首先,邮件的发送方要开启POP3 和SMTP服务--即发送qq邮件的账号要开启POP3 和SMTP服务

2.开启方法:

登陆qq邮箱

3.点击 设置

4.点击—-账户

5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启

6.送短信 —–点击确定

7.稍等一会,很得到一个授权码! –注意:这个一定要记住,一会用到

8.点击保存修改 —OK 完成

9.java 测试代码:

package cn.cupcat.test;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMessage.RecipientType;

public class SendmailUtil {

public static void main(String[] args) throws AddressException, MessagingException {

Properties properties = new Properties();

properties.put("mail.transport.protocol", "smtp");// 连接协议

properties.put("mail.smtp.host", "smtp.qq.com");// 主机名

properties.put("mail.smtp.port", 465);// 端口号

properties.put("mail.smtp.auth", "true");

properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用

properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息

//得到回话对象

Session session = Session.getInstance(properties);

// 获取邮件对象

Message message = new MimeMessage(session);

//设置发件人邮箱地址

message.setFrom(new InternetAddress("123456789@qq.com"));

//设置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });

//设置邮件标题

message.setSubject("这是第一封Java邮件");

//设置邮件内容

message.setText("内容为: 这是第一封java发送来的邮件。");

//得到邮差对象

Transport transport = session.getTransport();

//连接自己的邮箱账户

transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密码为刚才得到的授权码

//发送邮件 transport.sendMessage(message, message.getAllRecipients());

}

}

10.运行就会发出邮件了。。。。

下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的,你们测试的时候,可以修改能你们自己的邮箱。最后,祝你也能成功,如果有什么问题,可以一起讨论!

注意事项

得到的授权码一定要保存好,程序中要使用

java实现发送邮件功能

要实现邮件发送功能需要导入包:mail.jar

/*

* Generated by MyEclipse Struts

* Template path: templates/java/JavaClass.vtl

*/

package org.demo.action;

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;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.demo.form.DemoForm;

public class DemoAction extends Action {

private static final String CONTENT_TYPE = "test/html;charset=GB2312";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

DemoForm demoForm = (DemoForm) form;

System.out.println("标题是" + demoForm.getBiaoti());

System.out.println("内容是" + demoForm.getNeirong());

try {

response.setContentType(CONTENT_TYPE);

String smtphost = "smtp.nj.headware.cn"; // 发送邮件服务器

String user = "q0000015369"; // 邮件服务器登录用户名

String password = "Queshuwen26"; // 邮件服务器登录密码

String from = "q0000015369@nj.headware.cn"; //

String to = "c0000016205@nj.headware.cn"; // 收件人邮件地址

String subject = demoForm.getBiaoti(); // 邮件标题

String body = demoForm.getNeirong(); // 邮件内容

Properties props = new Properties();

props.put("mail.smtp.host", smtphost);

props.put("mail.smtp.auth", "true");

Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);

message.setFrom(fromAddress);

InternetAddress toAddress = new InternetAddress(to);

message.addRecipient(Message.RecipientType.TO, toAddress);

message.setSubject(subject);

message.setText(body);

Transport transport = ssn.getTransport("smtp");

transport.connect(smtphost, user, password);

transport.sendMessage(message, message

.getRecipients(Message.RecipientType.TO));

// transport.send(message);

transport.close();

return mapping.findForward("succ");

} catch (Exception e) {

e.printStackTrace();

return mapping.findForward("fail");

}

}

}

Java发邮件的几种方式

下面给你介绍3种发送邮件的方式:

1:使用JavaMail发送邮件

2:发送文本邮件

3:发送 HTML 格式的邮件

怎样用java实现邮件的发送?

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

import java.net.SocketException;

import java.rmi.UnknownHostException;

import java.util.StringTokenizer;

import sun.misc.BASE64Encoder;

public class Sender {

//private boolean debug = true;

BASE64Encoder encode=new BASE64Encoder();//用于加密后发送用户名和密码

static int dk=25;

private Socket socket;

public Sender(String server, int port) throws UnknownHostException,

IOException {

try {

socket = new Socket(server, dk);

} catch (SocketException e) {

System.out.println(e.getMessage());

} catch (Exception e) {

e.printStackTrace();

} finally {

//System.out.println("已经建立连接!");

}

}

// 注册到邮件服务器

public void helo(String server, BufferedReader in, BufferedWriter out)

throws IOException {

int result;

result = getResult(in);

// 连接上邮件服务后,服务器给出220应答

if (result != 220) {

throw new IOException("连接服务器失败");

}

result = sendServer("HELO " + server, in, out);

// HELO命令成功后返回250

if (result != 250) {

throw new IOException("注册邮件服务器失败!");

}

}

private int sendServer(String str, BufferedReader in, BufferedWriter out)

throws IOException {

out.write(str);

out.newLine();

out.flush();

/*

if (debug) {

System.out.println("已发送命令:" + str);

}

*/

return getResult(in);

}

public int getResult(BufferedReader in) {

String line = "";

try {

line = in.readLine();

/*

if (debug) {

System.out.println("服务器返回状态:" + line);

}

*/

} catch (Exception e) {

e.printStackTrace();

}

// 从服务器返回消息中读出状态码,将其转换成整数返回

StringTokenizer st = new StringTokenizer(line, " ");

return Integer.parseInt(st.nextToken());

}

public void authLogin(MailMessage message, BufferedReader in,

BufferedWriter out) throws IOException {

int result;

result = sendServer("AUTH LOGIN", in, out);

if (result != 334) {

throw new IOException("用户验证失败!");

}

result=sendServer(encode.encode(message.getUser().getBytes()),in,out);

//System.out.println("用户名: "+encode.encode(message.getUser().getBytes()));

if (result != 334) {

throw new IOException("用户名错误!");

}

result=sendServer(encode.encode(message.getPassword().getBytes()),in,out);

//result=sendServer(message.getPassword(),in,out);

//System.out.println("密码: "+encode.encode(message.getPassword().getBytes()));

if (result != 235) {

throw new IOException("验证失败!");

}

}

// 开始发送消息,邮件源地址

public void mailfrom(String source, BufferedReader in, BufferedWriter out)

throws IOException {

int result;

result = sendServer("MAIL FROM:" + source + "", in, out);

if (result != 250) {

throw new IOException("指定源地址错误");

}

}

// 设置邮件收件人

public void rcpt(String touchman, BufferedReader in, BufferedWriter out)

throws IOException {

int result;

result = sendServer("RCPT TO:" + touchman + "", in, out);

if (result != 250) {

throw new IOException("指定目的地址错误!");

}

}

// 邮件体

public void data(String from, String to, String subject, String content,

BufferedReader in, BufferedWriter out) throws IOException {

int result;

result = sendServer("DATA", in, out);

// 输入DATA回车后,若收到354应答后,继续输入邮件内容

if (result != 354) {

throw new IOException("不能发送数据");

}

out.write("From: " + from);

out.newLine();

out.write("To: " + to);

out.newLine();

out.write("Subject: " + subject);

out.newLine();

out.newLine();

out.write(content);

out.newLine();

// 句号加回车结束邮件内容输入

result = sendServer(".", in, out);

//System.out.println(result);

if (result != 250) {

throw new IOException("发送数据错误");

}

}

// 退出

public void quit(BufferedReader in, BufferedWriter out) throws IOException {

int result;

result = sendServer("QUIT", in, out);

if (result != 221) {

throw new IOException("未能正确退出");

}

}

// 发送邮件主程序

public boolean sendMail(MailMessage message, String server) {

try {

BufferedReader in = new BufferedReader(new InputStreamReader(

socket.getInputStream()));

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(

socket.getOutputStream()));

helo(server, in, out);// HELO命令

authLogin(message, in, out);// AUTH LOGIN命令

mailfrom(message.getFrom(), in, out);// MAIL FROM

rcpt(message.getTo(), in, out);// RCPT

data(message.getDatafrom(), message.getDatato(),

message.getSubject(), message.getContent(), in, out);// DATA

quit(in, out);// QUIT

} catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

}

}

再写一个MailMessage.java,set/get方法即可。

怎么用java发送邮件,像园子那样

1.首先你需要一个邮箱中转发送站(听着很高端的样子),说白了就是注册一个邮箱作为你的发送邮件平台,然后通过编程调用平台发送邮件(也就是你注册某个邮箱,然后开通SMTP/POP3协议,在编程中,拿着你的KEY去发送邮件),我试过很多种邮箱,QQ貌似不能用,网易经常报错,建议用新浪的,我用基本没出过问题。

2.去网上下载java开源的发送邮件工具类:mail.jar,并导入myeclipse/eclipse的引用。

3.编程(工具类)

关于java邮件传输和javamail发送邮件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。