使用R语言send.mail发送邮件时出现Java EmailException错误求助
Troubleshooting the EmailException When Using
send.mail() in R Let’s break down why you’re hitting this error and fix it step by step—first addressing code mistakes, then diving into Gmail’s specific security hoops.
1. Fix Syntax & Formatting Errors in Your Code
Your code has a few obvious typos and syntax gaps that are contributing to the failure:
- Invalid recipient email: You wrote
recieve1234@gmailcom—missing the dot beforecom. It should berecieve1234@gmail.com. - Missing comma in the SMTP list: After
ssl = TRUE), you need a comma beforeauthenticate = FALSE. Right now, R is reading this as invalid syntax. - Empty authentication credentials: Even if you set
authenticate = FALSE, Gmail’s SMTP server requires valid credentials for almost all use cases. Leavinguser.nameandpasswdblank will fail authentication.
Here’s the corrected code skeleton to start with:
send.mail( from = "test1234@gmail.com", to = "recieve1234@gmail.com", # Fixed the missing dot subject = "test_email", body = "hi", smtp = list( host.name = "smtp.gmail.com", port = 465, user.name = "test1234@gmail.com", # Add your full Gmail address passwd = "your_app_specific_password", # Use app password (see below) ssl = TRUE ), # Added comma here to fix syntax authenticate = TRUE, # Gmail requires authentication send = TRUE )
2. Resolve Gmail’s SMTP Security Restrictions
Google blocks standard password access to SMTP by default for security reasons. You’ll need to adjust your account settings:
- Enable 2-Step Verification (2SV): Head to your Google Account’s Security settings and turn on 2SV. This is mandatory to create an App Password.
- Generate an App Password: Once 2SV is active, go to the "App Passwords" section (under Security). Create a new password for "Mail" and your device. Use this password as the
passwdvalue in your code—not your regular Gmail password. - For personal accounts without 2SV: You can temporarily enable "Less secure app access" (though Google strongly discourages this long-term). Note this option disappears if you turn on 2SV.
- Work/Education accounts: If you’re using Google Workspace, check with your admin to ensure SMTP access is allowed for your account.
3. Check for Network/Firewall Blocks
- Port restrictions: Your local firewall or network might block port 465. Try switching to port 587 with TLS instead: update
port = 587and replacessl = TRUEwithtls = TRUEin the SMTP list. - Antivirus interference: Some antivirus programs block outgoing SMTP connections. Temporarily disable it to test if that’s the culprit.
By fixing these code errors and updating your Gmail security settings, you should be able to send emails successfully with send.mail().
内容的提问来源于stack exchange,提问作者Abhishek Gupta
相关产品推荐
相关产品推荐

