java邮件短信代码 java发送邮件的简单实现

如何使用Java发送qq邮件

方法:

创新互联公司-专业网站定制、快速模板网站建设、高性价比睢阳网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式睢阳网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖睢阳地区。费用合理售后完善,十年实体公司更值得信赖。

1.前提准备工作:

首先,邮件的发送方要开启POP3 和SMTP服务--即发送qq邮件的账号要开启POP3 和SMTP服务

2.开启方法:

登陆qq邮箱

3.点击 设置

4.点击—-账户

5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 —点击开启

6.送短信 —–点击确定

7.稍等一会,很得到一个授权码! –注意:这个一定要记住,一会用到

8.点击保存修改 —OK 完成

9.java 测试代码:

package cn.cupcat.test;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMessage.RecipientType;

public class SendmailUtil {

public static void main(String[] args) throws AddressException, MessagingException {

Properties properties = new Properties();

properties.put("mail.transport.protocol", "smtp");// 连接协议

properties.put("mail.smtp.host", "smtp.qq.com");// 主机名

properties.put("mail.smtp.port", 465);// 端口号

properties.put("mail.smtp.auth", "true");

properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用

properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息

//得到回话对象

Session session = Session.getInstance(properties);

// 获取邮件对象

Message message = new MimeMessage(session);

//设置发件人邮箱地址

message.setFrom(new InternetAddress("123456789@qq.com"));

//设置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("987654321@qq.com") });

//设置邮件标题

message.setSubject("这是第一封Java邮件");

//设置邮件内容

message.setText("内容为: 这是第一封java发送来的邮件。");

//得到邮差对象

Transport transport = session.getTransport();

//连接自己的邮箱账户

transport.connect("123456789@qq.com", "vvctybgbvvophjcj");//密码为刚才得到的授权码

//发送邮件 transport.sendMessage(message, message.getAllRecipients());

}

}

10.运行就会发出邮件了。。。。

下面是我收到邮件的截图,当然我把源码中的邮件地址都是修改了,不是真实的,你们测试的时候,可以修改能你们自己的邮箱。最后,祝你也能成功,如果有什么问题,可以一起讨论!

注意事项

得到的授权码一定要保存好,程序中要使用

用Java调用短信接口给手机发送短信怎么写代码??接口已经有了,是一个url.

那你要做的就是向这个URL发送数据就好,给个例子:

public   class  Httptest1  {

8  

9       public   static   void  main(String[] args)  {

10  

11         URL url  =   null ;

12         HttpURLConnection conn  =   null ;

13         OutputStream outStream  =   null ;

14         InputStream inStream  =   null ;

15         

16           try   {

17             url  =   new  URL( "  " );

18             conn  =  (HttpURLConnection) url.openConnection();

19             conn.setDoOutput( true );

20             conn.setDoInput( true );

21  

22             String sendXml  =   " ?xml version=\ " 1.0 \ "  encoding=\ " UTF - 16 \ " ?!DOCTYPE SigMailCommand SYSTEM \ " SigMail.dtd\ " " ;  // XML数据     

23              sendXml  +=   " SigMailCommand Name=\ " checkuser\ " Param Name=\ " domainname\ " test.com.cn/Param " ;

24             sendXml  +=   " Param Name=\ " username\ " admin/Param/SigMailCommand " ;

25  

26             outStream  =  conn.getOutputStream();

27  

28              // 准备通过CONN对象写入XML数据 

29              BufferedWriter bw  =   new  BufferedWriter( new  java.io.OutputStreamWriter(outStream,

30              " UTF-16 " ));

31             bw.write(sendXml);

32             bw.flush();

33             bw.close();

34             

35              // DataOutputStream dataOutStream = new DataOutputStream(outStream);

36              // dataOutStream.writeChars(xml);

37              // dataOutStream.flush();

38              // dataOutStream.close();

39             

40  

41              // 准备通过CONN对象读取返回的XML数据 

42  

43             inStream  =  conn.getInputStream();

44             StringBuffer returnXml  =   new  StringBuffer( "" );

45             BufferedReader rd  =   new  BufferedReader( new  InputStreamReader(inStream,

46                      " UTF-16 " ));

47               for  (String line  =   null ; (line  =  rd.readLine())  !=   null ;)  {

48                 returnXml.append(line);

49             } 

50             System.out.println(returnXml.toString());

51             rd.close();

52          }   catch  (IOException ex)  {

53             ex.printStackTrace();

54          } finally {

55               try   {

56                  if  (outStream  !=   null )

57                     outStream.close();

58                  if  (inStream  !=   null )

59                     inStream.close();

60                  if  (conn  !=   null )

61                     conn.disconnect();

62              }   catch  (IOException e)  {

63                  //  TODO 自动生成 catch 块 

64                  e.printStackTrace();

65             } 

66         } 

67  

68     } 

69  

70 }

用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("12345367@qq.com"); //邮件接收地址

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

javamail 回复邮件代码

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

/**

* pTitle: java mail/p

* pDescription: /p

* pCopyright: Copyright (c) 2004/p

* pCompany: /p

* @author petehero

* @version 1.0

*/

class SendMail

{

/**

* Logger for this class

*/

private static String server = "smtp.163.com";//smtp主机名。

private static String from = "XXXXXX@163.com";//发送方邮件地址

private static String pw = "XXXXXX";//发送方邮件密码。

private static String to = "XXXXXX@163.com"; //接收方邮件地址

static String subject="hello";

/*BodyPart bp = new MimeBodyPart();

Multipart mp = new MimeMultipart();

DataSource source = new FileDataSource("a.jpg");//附件

*/

static String mailContent="你好!\n\n"+"\t邮件正文test\n\n";//邮件正文

public static void send()

{

try

{

Properties props = new Properties();

Session sendMailSession;

Store store;

Transport transport;

props.put("mail.smtp.auth","true");

props.put("mail.smtp.host",server);

props.put("mail.smtp.user",from);

props.put("mail.smtp.password",pw);

//PopupAuthenticator popA=new PopupAuthenticator();

//PasswordAuthentication pop = popA.performCheck(from, pw);

//sendMailSession = Session.getInstance(props,popA);//填写用户名及密码

sendMailSession = Session.getInstance(props,new Authenticator(){

public PasswordAuthentication getPasswordAuthentication() //邮件安全认证。

{

return new PasswordAuthentication(from, pw);

}

});

Message newMessage = new MimeMessage(sendMailSession);

newMessage.setFrom(new InternetAddress(from));

newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

newMessage.setSubject(subject);

newMessage.setSentDate(new Date());

/*

bp.setText("bptext");

mp.addBodyPart(bp);

bp = new MimeBodyPart();

bp.setDataHandler(new DataHandler(source));

bp.setFileName("141.txt");

mp.addBodyPart(bp);

newMessage.setContent(mp);

*/

mailContent+=new Date().toLocaleString();

newMessage.setText(mailContent);

transport = sendMailSession.getTransport("smtp");

//System.out.println("Sending...");

//transport

Transport.send(newMessage);

//Transport.send(newMessage );

//System.out.println(server+from+pw+to);

}

catch (MessagingException ex)

{

}

}

// public static void main(String[] args)

// {

//

//// for(int i=0;iargs.length;i++)

//// {

//// System.out.println(args[i]);

//// }

//

// from=args[0].trim();

////

// pw=args[1].trim();

////

// SendMail sml = new SendMail();

// sml.send();

//// String a=SendMail.mailSend("hello","world");

//// System.out.println(a);

// }

static String mailSend(String t_to,String t_subject,String t_text)

{ String state="";

to=t_to;

subject=t_subject;

mailContent=t_text+"\n\n";

SendMail sml = new SendMail();

sml.send();

state="Successful";

return state;

}

/* public class PopupAuthenticator extends Authenticator{

String username=null;

String password=null;

public PopupAuthenticator(){}

public PasswordAuthentication performCheck(String user,String pass){

username = user;

password = pass;

return getPasswordAuthentication();

}

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(username, password);

}

}*/

}

Java发送邮件

JAVA邮件发送的大致过程是这样的的:

1、构建一个继承自javax.mail.Authenticator的具体类,并重写里面的getPasswordAuthentication()方法。此类是用作登录校验的,以确保你对该邮箱有发送邮件的权利。

2、构建一个properties文件,该文件中存放SMTP服务器地址等参数。

3、通过构建的properties文件和javax.mail.Authenticator具体类来创建一个javax.mail.Session。Session的创建,就相当于登录邮箱一样。剩下的自然就是新建邮件。

4、构建邮件内容,一般是javax.mail.internet.MimeMessage对象,并指定发送人,收信人,主题,内容等等。

5、使用javax.mail.Transport工具类发送邮件。

如何用Java实现短信自动发送功能?

Java实现短信自动发送功能主要是用httpclient实现的,要有发短信的端口。

1、硬件设备是一个3G网卡;

2、软件方面需要sun提过的java底层通信common包;

3、此外还需要第三方库SMSLib,这个是开源项目,主要用于实现java发短信的功能;

主要代码如下:

HttpClient client = new HttpClient();

PostMethod post = new PostMethod(

"");

post.addRequestHeader("Content-Type",

"application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码

NameValuePair[] data = { new NameValuePair("sname", "*****"),

new NameValuePair("spwd", "*****"),

new NameValuePair("scorpid", "*****"),

new NameValuePair("sprdid", "*****"),

new NameValuePair("sdst", "*****"),

new NameValuePair("smsg", "*****") };

post.setRequestBody(data);

client.executeMethod(post);

Header[] headers = post.getResponseHeaders();

int statusCode = post.getStatusCode();

System.out.println("statusCode:" + statusCode);

for (Header h : headers) {

System.out.println(h.toString());

}

String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));

System.out.println(result);

post.releaseConnection();

Java源程序(.java文件)——java字节码文件(.class文件)——由解释执行器(java.exe)将字节码文件加载到java虚拟机(jvm)——字节码文件(.class)就会在java虚拟机中执行。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程   。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点  。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等


本文名称:java邮件短信代码 java发送邮件的简单实现
标题网址:http://pwwzsj.com/article/hgohci.html