About Gmail SMTP Setting

About Gmail SMTP Setting

 

Gmail SMTP server address: smtp.gmail.com
Gmail SMTP user name: Your full Gmail address (e.g. me@gmail.com)
Gmail SMTP password: Your Gmail password
Gmail SMTP port: 465
Gmail SMTP TLS/SSL required: yes


Notice: TLS/SSL, 两个选一个就行了

### Java Gmail SMTP Email Sending Configuration Example To send emails through Gmail's SMTP service using a Java program, one must configure an appropriate `Properties` object and utilize the JavaMail API. Below is a comprehensive guide with code examples. #### Configuring Properties Object The following properties need to be set when configuring the connection to Gmail’s SMTP server: - **mail.smtp.host**: Specifies the host of the SMTP server. - **mail.smtp.port**: Defines the port number used by the SMTP server (typically 587 for TLS). - **mail.smtp.auth**: Indicates that authentication is required. - **mail.smtp.starttls.enable**: Enables STARTTLS encryption for secure communication between client and server. Here is how these settings look in practice: ```java Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); // Specify Gmail as the SMTP server[^3]. props.put("mail.smtp.port", "587"); // Use port 587 for TLS/STARTTLS[^4]. props.put("mail.smtp.auth", "true"); // Enable authentication. props.put("mail.smtp.starttls.enable", "true");// Enable STARTTLS. ``` #### Authenticating with Gmail Credentials Authentication requires providing valid credentials such as username (email address) and password or app-specific passwords if two-factor authentication is enabled on the account. Below demonstrates creating a session with authenticator details: ```java import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) throws MessagingException { final String username = "your-email@gmail.com"; final String password = "your-password"; Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props, new Authenticator() { // Provide authentication mechanism. protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); Message message = prepareMessage(session); // Prepare email content. Transport.send(message); // Send the prepared message. System.out.println("Sent message successfully."); } private static Message prepareMessage(Session session) throws MessagingException { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from-address@example.com")); // Set sender address. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to-address@example.com")); // Add recipient(s). message.setSubject("Test Subject"); // Define subject line. message.setText("This is a test email body."); // Write text content. return message; } } ``` #### Handling Errors Effectively When dealing with potential issues during email transmission, consider implementing proper exception handling mechanisms within your application logic. For instance, catching exceptions related to network connectivity problems or invalid login attempts ensures robustness against runtime failures. Additionally, logging detailed information about encountered errors may assist debugging efforts significantly. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值