java email怎么设置端口号_java发送邮件基础方法(另附部分主流邮箱服务器地址、端口及授权码设置方法)...

importjava.io.File;importjava.io.UnsupportedEncodingException;importjava.util.Properties;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;importjavax.mail.Authenticator;importjavax.mail.BodyPart;importjavax.mail.Message.RecipientType;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjavax.mail.internet.MimeUtility;public classMailUtil {/*** @Description: 使用QQ邮箱发送不带附件的邮件,收件人邮箱类型不限。

* @date: 2019年12月17日 下午4:51:01

*@author: ggwudivs

*@paramsubject 邮件标题

*@paramcontent 邮件内容

*@paramfromUser 发件人邮箱

*@paramfromPass 发件人邮箱密码,应为16位SMTP口令

*@paramtO_Recipients 收件人邮箱,多个收件人用","分隔

*@paramopenSSL 是否开启SSL

*@throwsUnsupportedEncodingException

*@throwsMessagingException:

*@return: void*/

public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, boolean openSSL) throwsUnsupportedEncodingException, MessagingException{

sendMessage(subject, content, fromUser, fromPass,null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", null, openSSL);

}/*** @Description: 使用QQ邮箱发送带附件的邮件,收件人邮箱类型不限。

* @date: 2019年12月17日 下午5:25:14

*@author: ggwudivs

*@paramsubject 邮件标题

*@paramcontent 邮件内容

*@paramfromUser 发件人邮箱

*@paramfromPass 发件人邮箱密码,应为16位SMTP口令

*@paramtO_Recipients 收件人邮箱,多个收件人用","分隔

*@paramattachmentFilesPath 邮件附件路径,多个附件用","分隔

*@paramopenSSL 是否开启SSL

*@throwsUnsupportedEncodingException

*@throwsMessagingException:

*@return: void*/

public static void sendMessage_QQ (String subject, String content, String fromUser, String fromPass, String tO_Recipients, String attachmentFilesPath, boolean openSSL) throwsUnsupportedEncodingException, MessagingException{

sendMessage(subject, content, fromUser, fromPass,null, tO_Recipients, null, null, "smtp.qq.com", openSSL?"465":"587", attachmentFilesPath, openSSL);

}/*** @Description: smtp发送邮件

* @date: 2019年12月17日 下午3:22:35

*@author: ggwudivs

*@paramsubject 邮件标题

*@paramcontent 邮件文本内容

*@paramfromUser 发件人邮箱

*@paramfromPass 发件人邮箱密码,QQ邮箱应为16位SMTP口令

*@paramnickname 发件人昵称

*@paramtO_Recipients 收件人邮箱,多个收件人用","分隔

*@paramcC_Recipients 抄送人邮箱,多个抄送人用","分隔

*@parambCC_Recipients 密送人邮箱,多个密送人用","分隔

*@paramsmtpHost smtp服务器地址

*@paramsmtpPort smtp服务器端口

*@paramattachmentFilesPath 邮件附件路径,多个附件用","分隔

*@throwsMessagingException

*@throwsUnsupportedEncodingException:

*@return: void*/

public static void sendMessage(String subject, String content, String fromUser, String fromPass, String nickname, String tO_Recipients, String cC_Recipients, String bCC_Recipients, String smtpHost, String smtpPort, String attachmentFilesPath, boolean openSSL) throwsMessagingException, UnsupportedEncodingException {//创建Properties类,用于记录邮箱的一些属性

Properties props = newProperties();//表示SMTP发送邮件,必须进行身份验证

props.put("mail.smtp.auth", "true");//SMTP服务器地址

props.put("mail.smtp.host", smtpHost);//是否开启SSL

if(!openSSL){//SMTP服务器端口号

props.put("mail.smtp.port", smtpPort);

}else{//SMTP服务器ssl端口号

props.setProperty("mail.smtp.socketFactory.port", smtpPort);

props.setProperty("mail.smtp.socketFactory.fallback", "false");

props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

}//构建授权信息,用于进行SMTP进行身份验证

Authenticator authenticator = newAuthenticator() {protectedPasswordAuthentication getPasswordAuthentication() {//发件人账号,发件人密码(QQ邮箱应为16位SMTP口令)

return newPasswordAuthentication(fromUser, fromPass);

}

};//使用环境属性和授权信息,创建邮件会话

Session mailSession =Session.getInstance(props, authenticator);//创建邮件消息

MimeMessage message = newMimeMessage(mailSession);//设置发件人,有昵称时同时设置昵称

try{

message.setFrom((nickname==null||"".equals(nickname))?new InternetAddress(fromUser):new InternetAddress(fromUser, nickname, "UTF-8"));

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}//设置一个或多个收件人

message.setRecipients(RecipientType.TO, tO_Recipients);//设置一个或多个抄送人

message.setRecipients(RecipientType.CC, cC_Recipients);//设置一个或多个密送人

message.setRecipients(RecipientType.BCC, bCC_Recipients);//设置邮件标题

message.setSubject(subject);//设置邮件的内容

if(attachmentFilesPath == null || "".equals(attachmentFilesPath)){//设置邮件的正文文本

message.setContent(content, "text/html;charset=UTF-8");

}else{//向multipart对象中添加邮件的各个部分内容,包括文本内容和附件

Multipart multipart = newMimeMultipart();//添加邮件文本内容

BodyPart contentBodyPart = newMimeBodyPart();

contentBodyPart.setContent(content,"text/html;charset=utf-8");

multipart.addBodyPart(contentBodyPart);//添加邮件附件内容

BodyPart attachmentBodyPart = newMimeBodyPart();

String[] attachmentFiles= attachmentFilesPath.split(",");for(String attachmentFile : attachmentFiles) {if (attachmentFile != null && !"".equals(attachmentFile)) {

attachmentBodyPart= newMimeBodyPart();//根据附件路径获取文件,

FileDataSource dataSource = new FileDataSource(newFile(attachmentFile));

attachmentBodyPart.setDataHandler(newDataHandler(dataSource));//MimeUtility.encodeWord可以避免文件名乱码

String strFileName=dataSource.getFile().getName();

attachmentBodyPart.setFileName(MimeUtility.encodeText(strFileName));

multipart.addBodyPart(attachmentBodyPart);

}

}//设置邮件的正文内容

message.setContent(multipart);

}//发送邮件

Transport.send(message);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java中的Socket类来监听某个端口和IP地址。下面是一个简单的示例代: ```java import java.net.*; import java.io.*; public class SocketListener { public static void main(String[] args) throws IOException { int port = 1234; // 监听的端口号 String ip = "127.0.0.1"; // 监听的IP地址 ServerSocket serverSocket = new ServerSocket(port, 50, InetAddress.getByName(ip)); System.out.println("Server started"); while (true) { Socket socket = serverSocket.accept(); System.out.println("Client connected"); InputStream inputStream = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String message = reader.readLine(); System.out.println("Received message: " + message); socket.close(); System.out.println("Client disconnected"); } } } ``` 在这个示例中,我们创建了一个ServerSocket对象来监听指定的IP地址端口号。然后我们使用accept()方法来等待客户端的连接请求。一旦客户端连接成功,我们就可以通过Socket对象获取输入流,并使用BufferedReader读取客户端发送过来的消息。 要向指定服务器的指定端口发送信息,你可以使用Java中的Socket类的另一个实例。下面是一个简单的示例代: ```java import java.net.*; import java.io.*; public class SocketSender { public static void main(String[] args) throws IOException { int port = 1234; // 目标端口号 String ip = "127.0.0.1"; // 目标IP地址 Socket socket = new Socket(ip, port); OutputStream outputStream = socket.getOutputStream(); PrintWriter writer = new PrintWriter(outputStream, true); writer.println("Hello, server!"); socket.close(); } } ``` 在这个示例中,我们创建了一个Socket对象来连接指定的IP地址端口号。然后我们可以通过Socket对象获取输出流,并使用PrintWriter向服务器发送消息。最后,记得关闭Socket对象。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值