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;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;看不到代碼其他暫時不清楚,不懂繼續問我
—— 神一樣的男人