导航:首页 > 编程语言 > java表格邮件

java表格邮件

发布时间:2023-06-05 21:27:42

⑴ 用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

⑵ java 发送邮件

要两个java文件 还有一个mail.jar是不是只能用javamail谁也不敢说

第一个:

public class Constant {

public static final String mailAddress ="用户名@163.com";
public static final String mailCount ="用户名";
public static final String mailPassword ="密码";
public static final String mailServer ="smtp.163.com";
//pukeyouxintest,

}

第二个:

import java.util.Date;
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;

public class SendMail {
/**
* 发送简单邮件
* @param str_from:发件人地址
* @param str_to:收件人地址
* @param str_title:邮件标题
* @param str_content:邮件正文
*/
public static void send(String str_from,String str_to,String str_title,String str_content) {

// str_content="<a href='www.163.com'>html元素</a>"; //for testing send html mail!

try {
//建立邮件会话
Properties props=new Properties(); //用来在一个文件中存储键-值对的,其中键和值是用等号分隔的,
//存储发送邮件服务器的信息
props.put("mail.smtp.host",Constant.mailServer);
//同时通过验证
props.put("mail.smtp.auth","true");
//根据属性新建一个邮件会话
Session s=Session.getInstance(props);
s.setDebug(true); //有他会打印一些调试信息。

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

//设置邮件
InternetAddress from= new InternetAddress(str_from); //[email protected]
message.setFrom(from); //设置发件人的地址
//
// //设置收件人,并设置其接收类型为TO
InternetAddress to=new InternetAddress(str_to); //[email protected]
message.setRecipient(Message.RecipientType.TO, to);

//设置标题
message.setSubject(str_title); //java学习

//设置信件内容
// message.setText(str_content); //发送文本邮件 //你好吗?
message.setContent(str_content, "text/html;charset=gbk"); //发送HTML邮件 //<b>你好</b><br><p>大家好</p>
//设置发信时间
message.setSentDate(new Date());

//存储邮件信息
message.saveChanges();

//发送邮件
Transport transport=s.getTransport("smtp");
//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
transport.connect(Constant.mailServer,Constant.mailCount,Constant.mailPassword);
//发送邮件,其中第二个参数是所有已设好的收件人地址
transport.sendMessage(message,message.getAllRecipients());
transport.close();

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
//测试用的,你吧你想写的内容写上去就行
send(Constant.mailAddress,"收件人邮箱","标题","<b>内容</b>");
}
}

然后把mail.jar导入,就可以了,我用的是163 的,其他的吧相应的服务器改一下就行了

⑶ 如何用java实现发送html格式的邮件

首先Java发送邮件需要用到JavaMail,先到Oracle官网上下载好最新版本的JavaMail(刚才看了一下,最新是1.5.3),把下载的这个jar文件放到classpath里(如果是Web项目,就放到WEB-INF/lib目录下。

JavaMail主要支持发送纯文本的和html格式的邮件。

发送html格式的邮件的一个例程如下:

importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeUtility;
importjavax.mail.Session;
importjavax.mail.MessagingException;
importjavax.mail.Transport;

publicclassSendHtmlMail{
publicstaticvoidsendMessage(StringsmtpHost,
Stringfrom,Stringto,
Stringsubject,StringmessageText)
throwsMessagingException,java.io.UnsupportedEncodingException{

//Step1:Configurethemailsession
System.out.println("Configuringmailsessionfor:"+smtpHost);
java.util.Propertiesprops=newjava.util.Properties();
props.setProperty("mail.smtp.auth","true");//指定是否需要SMTP验证
props.setProperty("mail.smtp.host",smtpHost);//指定SMTP服务器
props.put("mail.transport.protocol","smtp");
SessionmailSession=Session.getDefaultInstance(props);
mailSession.setDebug(true);//是否在控制台显示debug信息

//Step2:Constructthemessage
System.out.println("Constructingmessage-from="+from+"to="+to);
InternetAddressfromAddress=newInternetAddress(from);
InternetAddresstoAddress=newInternetAddress(to);

MimeMessagetestMessage=newMimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
testMessage.setSentDate(newjava.util.Date());
testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));

testMessage.setContent(messageText,"text/html;charset=gb2312");
System.out.println("Messageconstructed");

//Step3:Nowsendthemessage
Transporttransport=mailSession.getTransport("smtp");
transport.connect(smtpHost,"webmaster","password");
transport.sendMessage(testMessage,testMessage.getAllRecipients());
transport.close();

System.out.println("Messagesent!");
}

publicstaticvoidmain(String[]args){

StringsmtpHost="localhost";
Stringfrom="[email protected]";
Stringto="[email protected]";
Stringsubject="html邮件测试";//subjectjavamail自动转码

StringBuffertheMessage=newStringBuffer();
theMessage.append("<h2><fontcolor=red>这倒霉孩子</font></h2>");
theMessage.append("<hr>");
theMessage.append("<i>年年失望年年望</i>");
try{
SendHtmlMail.sendMessage(smtpHost,from,to,subject,theMessage.toString());
}
catch(javax.mail.MessagingExceptionexc){
exc.printStackTrace();
}
catch(java.io.){
exc.printStackTrace();
}
}
}


JavaMail是封装了很多邮件操作的,所以使用起来不很困难,建议你到JavaMail官网看一下API或下载Java Doc API文档。

⑷ 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如何实现复制excel中内容并粘贴到邮件发

主要是用到java里面的i/o流。代码例子如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* java读写文件,复制文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter("f:\\text.txt", true);
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.txt"), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " + line);
fw.write(line);
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
rwFile();
}
}
首先在D盘新建文件1.txt,输入任意内容。然后执行java代码即可。

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

⑺ java 发送邮件,内容是要从数据库中读取的数据并列成表格的状态发送出去

publicbooleansendTextMail(MailSenderInfomailInfo){
//判断是否需要身份认证
MyAuthenticatorauthenticator=null;
Propertiespro=mailInfo.getProperties();
if(mailInfo.isValidate()){
//如果需要身份认证,则创建一个密码验证器
authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
//根据邮件会话属性和密码验证器构造一个发送邮件的session
SessionsendMailSession=null;


//sendMailSession=Session.getDefaultInstance(pro,authenticator);//获取默认可能报错
sendMailSession=Session.getInstance(pro,authenticator);//新创建一个session
if(sendMailSession==null){
System.out.println("无法获取邮件邮件Session");
}
try{
//根据session创建一个邮件消息
MessagemailMessage=newMimeMessage(sendMailSession);
//创建邮件发送者地址
Addressfrom=newInternetAddress(mailInfo.getFromAddress());
//设置邮件消息的发送者
mailMessage.setFrom(from);
//创建邮件的接收者地址,并设置到邮件消息中
Addressto=newInternetAddress(mailInfo.getToAddress());

mailMessage.setRecipient(Message.RecipientType.TO,to);

//设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
//设置邮件消息发送的时间
mailMessage.setSentDate(newDate());
//设置邮件消息的主要内容
StringmailContent=mailInfo.getContent();
mailMessage.setText(mailContent);//添加附件
//if(mailInfo.getAttachFileNames()!=null||mailInfo.getAttachFileNames().length>0){
//Multipartmp=newMimeMultipart();
//MimeBodyPartmbp=null;
//for(StringfileName:mailInfo.getAttachFileNames()){
//mbp=newMimeBodyPart();
//FileDataSourcefds=newFileDataSource(fileName);//得到数据源
//mbp.setDataHandler(newDataHandler(fds));//得到附件本身并至入BodyPart
//mbp.setFileName(fds.getName());//得到文件名同样至入BodyPart
//mp.addBodyPart(mbp);
//}
//mailMessage.setContent(mp);
//}

//发送邮件
Transport.send(mailMessage);
returntrue;
}catch(MessagingExceptionex){
ex.printStackTrace();

}
returnfalse;
}
publicclassMailSenderInfo{
//发送邮件的服务器的IP和端口
privateStringmailServerHost;
privateStringmailServerPort="25";
//邮件发送者的地址
privateStringfromAddress;
//邮件接收者的地址
privateStringtoAddress;
//登陆邮件发送服务器的用户名和密码
privateStringuserName;
privateStringpassword;
//是否需要身份验证
privatebooleanvalidate=false;
//邮件主题
privateStringsubject;
//邮件的文本内容
privateStringcontent;
//邮件附件的文件名
privateString[]attachFileNames;

//邮件抄送人

privateList<String>ccUserList;
/***//**
*获得邮件会话属性
*/
publicPropertiesgetProperties(){
Propertiesp=newProperties();
p.put("mail.smtp.host",this.mailServerHost);
p.put("mail.smtp.port",this.mailServerPort);
p.put("mail.smtp.auth",validate?"true":"false");
returnp;
}
publicStringgetMailServerHost(){
returnmailServerHost;
}
publicvoidsetMailServerHost(StringmailServerHost){
this.mailServerHost=mailServerHost;
}
publicStringgetMailServerPort(){
returnmailServerPort;
}
publicvoidsetMailServerPort(StringmailServerPort){
this.mailServerPort=mailServerPort;
}
publicbooleanisValidate(){
returnvalidate;
}
publicvoidsetValidate(booleanvalidate){
this.validate=validate;
}
publicString[]getAttachFileNames(){
returnattachFileNames;
}
publicvoidsetAttachFileNames(String[]fileNames){
this.attachFileNames=fileNames;
}
publicStringgetFromAddress(){
returnfromAddress;
}
publicvoidsetFromAddress(StringfromAddress){
this.fromAddress=fromAddress;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetToAddress(){
returntoAddress;
}
publicvoidsetToAddress(StringtoAddress){
this.toAddress=toAddress;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetSubject(){
returnsubject;
}
publicvoidsetSubject(Stringsubject){
this.subject=subject;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(StringtextContent){
this.content=textContent;
}
publicList<String>getCcUserList(){
returnccUserList;
}
publicvoidsetCcUserList(List<String>ccUserList){
this.ccUserList=ccUserList;
}


}

publicstaticvoidmain(String[]args){
//这个类主要是设置邮件
MailSenderInfomailInfo=newMailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("[email protected]");
mailInfo.setPassword("zzzong0828");//您的邮箱密码
mailInfo.setFromAddress("");
//接受方信息
mailInfo.setToAddress("");
mailInfo.setSubject("邮箱标题");
mailInfo.setContent("设置邮箱内容如http://www.guihua.org中国桂花网是中国最大桂花网站==");

String[]files=newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"};
mailInfo.setAttachFileNames(files);
//这个类主要来发送邮件
SimpleMailSendersms=newSimpleMailSender();
sms.sendTextMail(mailInfo);//发送文体格式
//sms.sendHtmlMail(mailInfo);//发送html格式
}



这样发

阅读全文

与java表格邮件相关的资料

热点内容
单片机16进制编程图 浏览:489
金刚2迅雷下载 浏览:275
聚优电影卡使用范围 浏览:760
浙江网络卫星授时服务器云空间 浏览:497
宝塔加密方式 浏览:217
linux环境变量的路径 浏览:749
粉笔缓存的视频在手机哪个文件夹 浏览:680
港片尺度大 浏览:373
女主胸大的H电影 浏览:877
小女孩那个电影叫什么 浏览:58
中越战争电影在哪看 浏览:896
成龙电影国语版全部 浏览:199
如何入侵网页服务器修改帐号 浏览:646
陕西物联网数显钟服务器云主机 浏览:279
原版3d是国语吗 浏览:926
程序员勇敢的第一步 浏览:160
安卓车载音乐什么格式 浏览:432
rin演过的电影 浏览:149
telnet命令登陆 浏览:328