简体   繁体   English

邮件发送问题

[英]Mail Sending problem

public class MailEx {
public static void main(String[] args) {
    try {

        String userName = "abc@gmail.com";          
        String password = "123";            
        String hostName = "smtp.gmail.com";
        String fromName = "Splendore Bkk";
        String to[] = {"xyz@gmail.com"};

        System.out.println("to.length::"+to.length);

        Properties props = new Properties();
        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.debug", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");     
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        System.out.println("to.length:sadfsadfds:"+to.length);
        // Get the default Session object.
        Session session = Session.getInstance(props);

        // Create a default MimeMessage object.
        MimeMessage message1 = new MimeMessage(session);

        // Set the RFC 822 "From" header field using the
        // value of the InternetAddress.getLocalAddress method.
        message1.setFrom(new InternetAddress(userName,fromName));

        Address[] addresses = new Address[to.length];
        for (int i = 0; i < to.length; i++) {
            Address address = new InternetAddress(to[i]);               
            addresses[i] = address;
            // Add the given addresses to the specified recipient type.
            message1.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
        }       
        // Set the "Subject" header field.
        message1.setSubject("Testing");

        // Sets the given String as this part's content,
        // with a MIME type of "text/plain".
        Multipart mp = new MimeMultipart("alternative");
        MimeBodyPart mbp = new MimeBodyPart();
        mbp.setContent("Hii from cc", "text/html");
        mp.addBodyPart(mbp);
        message1.setContent(mp);
        message1.saveChanges();

        // Send message
        Transport transport = session.getTransport("smtp");
        transport.connect(hostName,userName,password);
        transport.sendMessage(message1,addresses);
        transport.close();


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

}

}

I getting the Error ....我收到错误....

DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: ..\Java\jdk1.6.0\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
    at javax.mail.Session.loadProvidersFromStream(Session.java:928)
    at javax.mail.Session.access$000(Session.java:174)
    at javax.mail.Session$1.load(Session.java:870)
    at javax.mail.Session.loadResource(Session.java:1084)
    at javax.mail.Session.loadProviders(Session.java:889)
    at javax.mail.Session.<init>(Session.java:210)
    at javax.mail.Session.getInstance(Session.java:249)
    at com.test.MailEx.main(MailEx.java:41)

So can u tell me what's the problem...所以你能告诉我有什么问题吗...

To avoid DEBUG warnings, create files javamail.providers, javamail.address.map, javamail.default.address.map, javamail.default.providers under为避免DEBUG警告,请在下面创建文件javamail.providers, javamail.address.map, javamail.default.address.map, javamail.default.providers

 (Program Files)\Java\jdk1.6.0\jre\lib\

folder.文件夹。

About the error, NoClassDefFoundError , well you simply didn't add JavaMail to your classpath.关于错误NoClassDefFoundError ,您只是没有将 JavaMail 添加到您的类路径中。 If you are using Eclipse, right click project, follow Build Path ≥ Add Libraries or something like that and add javamail's jar file (which you should locate under your lib/ folder) to classpath of your project.如果您使用 Eclipse,请右键单击项目,按照 Build Path ≥ Add Libraries 或类似的操作,将 javamail 的jar文件(您应该位于lib/文件夹下)添加到您的项目的类路径中。

I fix it , look how my code is writing我修好了,看看我的代码写得怎么样

must to approve before in your google account Set Allow less secure apps to ON example how i did it https://stackoverflow.com/a/64023055/2347210必须在您的 google 帐户中批准之前将允许安全性较低的应用程序设置ON示例我是如何做到的https://stackoverflow.com/a/64023055/2347210
Use javax.mail version 1.4.7使用 javax.mail 版本 1.4.7

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public void SendEmail() throws Exception {
String EmailUsername = "MYMAIL@gmail.com";
String PasswordUsername = "SomePassword";

    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 javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(EmailUsername, PasswordUsername);
        }
    });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(EmailUsername));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("Example@gmail.com"));
        message.setSubject("This is the Qa test!");
        message.setText("this is the report");
        Transport.send(message);

        System.out.println("Message Sent!");

    } catch (MessagingException mex) {
        mex.printStackTrace();
    }
}

See this this have solution看到这个有解决方案

my ans check it out 我的回答检查出来

this is having desired solution for your problem这是您的问题所需的解决方案

Hope that will help.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM