标签: jakarta-mail

将JavaMail与TLS结合使用

我在SO上发现了关于JavaMail API和通过SMTP服务器发送邮件的其他几个问题,但没有人讨论过使用TLS安全性.我正在尝试使用JavaMail通过我的工作SMTP邮件服务器向我自己发送状态更新,但它需要TLS,我无法在线找到有关如何使用JavaMail访问需要TLS加密的SMTP服务器的任何示例.有人能帮忙吗?

java ssl smtp jakarta-mail

46
推荐指数
5
解决办法
11万
查看次数

utf 8 charset不适用于javax邮件

我使用Javax Mail API发送电子邮件.我使用联系方式发送输入,必须将其发送到特定的电子邮件.

发送电子邮件没有问题,虽然我是一个丹麦人,因此我需要三个丹麦字符,即'æ','ø'和'å',在主题和电子邮件文本中.

因此我看到我可以使用UTF-8字符编码来提供这些字符,但是当我的邮件发送时我只看到一些奇怪的字母 - 'ã|','ã¸'和'ã¥' - 而不是丹麦语字母 - 'æ','ø'和'å'.

我发送电子邮件的方法看起来像这样

public void sendEmail(String name, String fromEmail, String subject, String message) throws AddressException, MessagingException, UnsupportedEncodingException, SendFailedException
{
    //Set Mail properties
    Properties props = System.getProperties();
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.port", "465");
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("my_username", "my_password");
        }
    });

    //Create the email with variable input
    MimeMessage mimeMessage = new MimeMessage(session);
    mimeMessage.setHeader("Content-Type", "text/plain; charset=UTF-8");
    mimeMessage.setFrom(new …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail utf-8 character-encoding

45
推荐指数
3
解决办法
6万
查看次数

Javamail无法将套接字转换为TLS GMail

我试图通过gmails SMTP Server使用JavaMail发送电子邮件.但是这段代码.

final String username = "mygmail@gmail.com";
    final String password = "mygmailpassword";

    Properties props = new Properties();
    props.put("mail.smtp.auth", true);
    props.put("mail.smtp.starttls.enable", true);
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("no-reply@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("test@gmail.com"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler,"
                + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
Run Code Online (Sandbox Code Playgroud)

返回此错误

Could …
Run Code Online (Sandbox Code Playgroud)

java email gmail jakarta-mail

45
推荐指数
11
解决办法
11万
查看次数

来自Maven的JavaMail API

我正在尝试升级到最新的Java Mail实用程序.

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

到(我的意图)

<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.5.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但我找不到1.5.1的邮件工件,

但我可以看到

<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么组ID发生了变化,如果我更改了1.5.1的组ID,我是否需要更改已经存在的所有邮件实现(包名更改和其他任何内容)以及com.sun.mailvs 之间的区别是javax.mail什么?

java jakarta-mail maven

40
推荐指数
1
解决办法
2万
查看次数

如何使用JavaMail获取邮件帐户中的可用文件夹列表

我正在使用JavaMail API连接到我的个人帐户.我在我创建的Gmail帐户中有文件夹(标签)列表+收件箱,草稿等默认文件夹.如何列出所有可用文件夹(默认和用户创建的文件夹)?

我可以使用此API访问特定文件夹:Folder inbox = store.getFolder("Inbox");.是否有其他API可以获取邮件帐户中可用的文件夹列表?

java gmail imap jakarta-mail

39
推荐指数
3
解决办法
6万
查看次数

显示Java邮件发件人的地址而不是他的名字

我正在尝试通过我的Java Mail应用程序向我的朋友发送邮件.我能够成功完成,但邮箱中的接收者列显示完整的电子邮件地址,而不是发件人的姓名.我尝试更改各种参数,但邮箱仍会显示完整的电子邮件地址,而不是发件人的姓名.

使用此方法发送消息:

 public void send(String key){
    String to=key;
    String from="mygmailid";
    String subject="wassp";
    String text="Hello";
    Properties props=new Properties();

    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.user", "myname");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    Session mailSession=Session.getDefaultInstance(props);
    Message simpleMessage=new MimeMessage(mailSession);

    InternetAddress fromAddress=null;
    InternetAddress toAddress=null;

    try{
        fromAddress=new InternetAddress(from);
        toAddress=new InternetAddress(to);
    }
    catch(AddressException e){
        e.printStackTrace();
    }

    try{
        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipient(RecipientType.TO,toAddress);
        simpleMessage.setSubject(subject);
        simpleMessage.setText(text);

        transport.connect("smtp.gmail.com",465, "myid@gmail.com", "mygmailpassword");
        transport.sendMessage(simpleMessage, simpleMessage.getAllRecipients());
        transport.close();  

    }
    catch(MessagingException e){
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

我将此方法称为:

public static void main(String[] args) {
    MailSender mailer=new MailSender();
    mailer.send("friendmail@gmail.com");
}
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail

36
推荐指数
3
解决办法
5万
查看次数

有没有解决谷歌禁用“不太安全的应用程序”的方法?

因此,自 5 月 31 日起,谷歌禁用了“不太安全的应用程序”选项,因此我一直在使用 Java 邮件 API,并且自更新以来我无法再使用 Gmail smtp 发送电子邮件。

这是我收到的错误:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials n13-20020a5d400d000000b0020ff7246934sm4970874wrp.95 - gsmtp
Run Code Online (Sandbox Code Playgroud)

我切换到 Outlook 邮件,似乎工作正常,但我想知道是否有办法使用 Gmail 帐户

安全性较低的应用和您的 Google 帐户

security email gmail jakarta-mail

34
推荐指数
3
解决办法
2万
查看次数

如何使用JavaMail将多个文件附加到电子邮件?

以下Java代码用于将文件附加到电子邮件.我想通过电子邮件发送多个文件附件.任何建议,将不胜感激.

public class SendMail {

    public SendMail() throws MessagingException {
        String host = "smtp.gmail.com";
        String Password = "mnmnn";
        String from = "xyz@gmail.com";
        String toAddress = "abc@gmail.com";
        String filename = "C:/Users/hp/Desktop/Write.txt";
        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, toAddress);
        message.setSubject("JavaMail Attachment");
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("Here's the file");
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        messageBodyPart = new MimeBodyPart(); …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail

33
推荐指数
3
解决办法
9万
查看次数

使用带有POP3协议的javamail检索所有未读电子邮件

我正在尝试访问我的Gmail帐户并从中检索所有未读电子邮件的信息.

我在引用很多链接后编写了我的代码.我给出了一些链接供参考.

使用Java通过GMail帐户发送和接收电子邮件

使用JavaMailAPI接收邮件的Java代码

为了测试我的代码,我创建了一个Gmail帐户.所以我从Gmail收到了4封邮件.我在检查邮件数量后运行我的应用程序.这显示了正确的结果.4封未读邮件.显示所有信息(例如日期,发件人,内容,主题等)

然后我登录到我的新帐户,阅读其中一封电子邮件并重新运行我的应用程序.现在未读消息的计数应为3,但显示"未读消息数:0"

我在这里复制代码.

public class MailReader

{

    Folder inbox;

    // Constructor of the calss.

    public MailReader() {
        System.out.println("Inside MailReader()...");
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        /* Set the mail properties */

        Properties props = System.getProperties();
        // Set manual Properties
        props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.pop3.socketFactory.fallback", "false");
        props.setProperty("mail.pop3.port", "995");
        props.setProperty("mail.pop3.socketFactory.port", "995");
        props.put("mail.pop3.host", "pop.gmail.com");

        try

        {

            /* Create the session and get the store for read the mail. */

            Session session = Session.getDefaultInstance(
                    System.getProperties(), null);

            Store store = session.getStore("pop3");

            store.connect("pop.gmail.com", 995, "abc@gmail.com", …
Run Code Online (Sandbox Code Playgroud)

java pop3 jakarta-mail

33
推荐指数
3
解决办法
6万
查看次数

解决错误javax.mail.AuthenticationFailedException

我不熟悉这个用java发送邮件的功能.发送电子邮件重置密码时收到错误.希望你能给我一个解决方案.

以下是我的代码:

public synchronized static boolean sendMailAdvance(String emailTo, String subject, String body)
{
    String host = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ADDRESS");
    String userName = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-USERNAME");
    String password = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PASSWORD");
    String port = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-PORT");
    String starttls = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-STARTTLS");
    String socketFactoryClass = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-SOCKET-CLASS");
    String fallback = AppConfigManager.getProperty("SENDER-EMAIL-SMTP-ALLOW-FALLBACK");                         

    try
    {
        java.util.Properties props = null;
        props = System.getProperties();
        props.put("mail.smtp.user", userName);
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.debug", "true");

        if(!"".equals(port))
        {
            props.put("mail.smtp.port", port);
            props.put("mail.smtp.socketFactory.port", port);
        }

        if(!"".equals(starttls))
            props.put("mail.smtp.starttls.enable",starttls);

        if(!"".equals(socketFactoryClass))                   
            props.put("mail.smtp.socketFactory.class",socketFactoryClass);

        if(!"".equals(fallback))
            props.put("mail.smtp.socketFactory.fallback", fallback);

        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(true);

        MimeMessage msg …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail

33
推荐指数
6
解决办法
16万
查看次数

标签 统计

jakarta-mail ×10

java ×9

gmail ×3

email ×2

character-encoding ×1

imap ×1

maven ×1

pop3 ×1

security ×1

smtp ×1

ssl ×1

utf-8 ×1