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");
}
}
}