导航:首页 > IDC知识 > java通过服务器发送邮件

java通过服务器发送邮件

发布时间:2021-03-21 06:38:22

1、怎样用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方法即可。

2、java mail 向局域网内的邮件服务器发送邮件

内网是自己搭建的邮件服务器么?我以前用过apache的开源邮件服务器james,也是只需要设置props.setProperty("mail.host", "smtp.mymail.com");就可以了,至于smtp.mymail.com是可以配置在邮件服务器里面的,跟ip没关系的

3、谁做过Java web服务器发送邮件!

三种方法,不知道你需要那种1、通过applet条用页面JS,想WEB服务器发送信息,也可以通过AJAX或者JS调用表单提交2、通过Socket直接与服务器产生通讯,不管这个服务器是不是WEB这种方法都可以3、通过Axis调用WEB服务器上的WebService

4、怎样用java发送邮件

首先下载 JavaMail jar 包,并导入到项目中。下载地址

编写代码,代码如下:

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class App45 {

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

Properties properties = System.getProperties();

properties.setProperty("mail.smtp.host", "邮件发送服务器");

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

Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {

// 设置发件人邮件帐号和密码
return new PasswordAuthentication("邮件帐号", "密码");
}
});

MimeMessage message = new MimeMessage(session);

// 设置发件人邮件地址
message.setFrom(new InternetAddress("发件人邮件地址"));

// 设置收件人邮件地址
message.addRecipient(Message.RecipientType.TO, new InternetAddress("收件人邮件地址"));

message.setSubject("这里是邮件主题。");

message.setText("这里是邮件内容。");

Transport.send(message);
}
}

5、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 = "[email protected]"; //
String to = "[email protected]"; // 收件人邮件地址
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");
}

}
}

6、java web使用 用MS的exchange邮件服务器发送邮件

我说你这种想法不对!
我不能给你提供具体的解决方案,我给你一个思路!
首先你需要Exchange 200x 中配置 SMTP 连接器;
然后使用JAVA的mail包写发邮件的程序。

7、用java写一个邮件发送代码

public boolean mainto()
{
boolean flag = true;

//建立邮件会话
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.qq.com");//存储发送邮件的服务器
pro.put("mail.smtp.auth","true"); //通过服务器验证

Session s =Session.getInstance(pro); //根据属性新建一个邮件会话
//s.setDebug(true);

//由邮件会话新建一个消息对象
MimeMessage message = new MimeMessage(s);

//设置邮件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;

try
{
fromAddr = new InternetAddress(451144426+"@qq.com"); //邮件发送地址
message.setFrom(fromAddr); //设置发送地址

toAddr = new InternetAddress("[email protected]"); //邮件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //设置接收地址

message.setSubject(title); //设置邮件标题
message.setText(content); //设置邮件正文
message.setSentDate(new Date()); //设置邮件日期

message.saveChanges(); //保存邮件更改信息

Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq.com", "451144426", "密码"); //服务器地址,邮箱账号,邮箱密码
transport.sendMessage(message, message.getAllRecipients()); //发送邮件
transport.close();//关闭

}
catch (Exception e)
{
e.printStackTrace();
flag = false;//发送失败
}

return flag;
}

这是一个javaMail的邮件发送代码,需要一个mail.jar

8、本地java代码实现了发送邮件的功能,但是把它部署到公司服务器上就报404错误,

是采用分布式吗?抄如果是分布式的袭话邮件业务和其他业务是分开的,邮件服务器还会用zookeeper做负载均衡(差不多是那个意思),soa架构中其实就是个注册中心,所以你的问题可能是你的注册中心开启了吗?先开注册中心,再开邮件服务器才好使,第二点可能是你的ip,本地的时候不插网线默认是localhost,连公司服务器,而且你们公司应该可以连到外网上吧,所以改没改配置文件呢?localhost改成127.0.0.1;看不到代码其他暂时不清楚,不懂继续问我
—— 神一样的男人

与java通过服务器发送邮件相关的知识