导航:首页 > 编程语言 > java发送邮件的host

java发送邮件的host

发布时间:2023-08-23 15:21:42

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

}
}

阅读全文

与java发送邮件的host相关的资料

热点内容
win10ime 浏览:271
手机号大数据保护停机是什么意思 浏览:81
两个苹果手机怎么隔空投送app 浏览:903
ps修改有褶皱的文件 浏览:417
javadbfreader 浏览:307
苹果手机数字代码是什么 浏览:66
驱动程序顺序安装脚本 浏览:665
word文件里怎样查重 浏览:219
mx5系统基带版本 浏览:184
ntlea全域通win10 浏览:171
qq怎么查看别人的收藏 浏览:135
地震三参数matlab程序 浏览:57
怎样给优盘文件加密软件 浏览:7
收拾文件有哪些小妙招 浏览:431
pdf文件去底网 浏览:253
win10重装系统需要格式化c盘吗 浏览:424
路由器trx文件 浏览:655
淘宝店铺数据包怎么做 浏览:195
win10键盘黏连 浏览:332
json如何生成表格 浏览:323

友情链接