1. java發送郵件時錯誤,試了好幾次,mail.jar和active.jar都導入了,是還有什麼沒有配置嗎,比如埠。。。
importjava.util.Date;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Properties;
importjavax.mail.Message.RecipientType;
importjavax.mail.MessagingException;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
/**
*郵件發送工具類
*@author
*
*/
publicclassMailUtils{
publicstaticfinalStringMAIL_TRANSPORT_PROTOCOL="smtp";//協議
publicstaticfinalStringMAIL_SMTP_HOST="smtp.youmail.com";//郵件伺服器
publicstaticfinalintMAIL_SMTP_PORT=587;//埠
publicstaticfinalStringMAIL_SMTP_AUTH="true";
/**
*郵件發送
*@paramtarget目標郵箱
*@paramcontent
*@return
*@author
*/
publicstaticMap<String,Object>sendEmail(Stringtarget,Stringcontent){
Map<String,Object>map=newHashMap<String,Object>();
map.put("status",-1);
Propertiesprop=newProperties();
prop.put("mail.transport.protocol",MailUtils.MAIL_TRANSPORT_PROTOCOL);//指定協議
prop.put("mail.smtp.host",MailUtils.MAIL_SMTP_HOST);//主機stmp.qq.com
prop.put("mail.smtp.port",MailUtils.MAIL_SMTP_PORT);//埠
prop.put("mail.smtp.auth",MailUtils.MAIL_SMTP_AUTH);
Sessionsession=Session.getDefaultInstance(prop);
Transporttrans=null;
try{
MimeMessagemessage=newMimeMessage(session);
message.setSubject("郵件主題");
message.setSentDate(newDate());
Stringnick=javax.mail.internet.MimeUtility.encodeText("nickname");//
message.setFrom(newInternetAddress(nick+"<[email protected]>"));//
message.setRecipient(RecipientType.TO,newInternetAddress(target));
message.setText(content);//簡單純文本郵件
message.saveChanges();
trans=session.getTransport();
trans.connect("郵箱","密碼");//發件郵箱(為@之前的部分比如[email protected]發只需要填123)
trans.sendMessage(message,message.getAllRecipients());
map.put("status",0);
}catch(Exceptione){
e.printStackTrace();
}finally{
if(trans!=null){
try{
trans.close();
}catch(MessagingExceptione){
e.printStackTrace();
}
}
}
returnmap;
}
}
2. 用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
3. 用java來發送帶附件的郵件
用java來發送帶附件的郵件要翻譯的。
4. java mail 向區域網內的郵件伺服器發送郵件
內網是自己搭建的郵件伺服器么?我以前用過apache的開源郵件伺服器james,也是只需要設置props.setProperty("mail.host", "smtp.mymail.com");就可以了,至於smtp.mymail.com是可以配置在郵件伺服器裡面的,跟ip沒關系的
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");
}
}
}