English 中文(简体)
如何召集 Spring Springmailsender Gmail
原标题:How to configure Spring JavaMailSenderImpl for Gmail
问题回答

这为我工作:

        <property name="host"><value>smtp.gmail.com</value></property>
        <property name="port"><value>587</value></property>
        <property name="protocol"><value>smtp</value></property>
        <property name="username"><value>${mail.username}</value></property>
        <property name="password"><value>${mail.password}</value></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.smtp.quitwait">false</prop>
            </props>
        </property>

我发现,“protocol”价值必须是“mtp”(而不是“smtps”)。

我花了一小时时间,寻找正确的环境,利用 j诈发送电子邮件,最后是这样做的。 我之所以这样说,是因为我找不到一个全面的例子,通过电子邮件与javamailsender发送,希望这将有助于想做同样事情的人:

STEP:

将下列环境添加到邮件中。 财产:

mail.protocol=smtp
mail.host=smtp.gmail.com
mail.port=465
mail.smtp.socketFactory.port=465
mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.debug=true
mail.smtp.starttls.required=true
mail.smtp.socketFactory.fallback=false
mail.from=XXX@gmail.com
mail.username=XXX@gmail.com
mail.password=my_password

之后,在您的邮件组别中,

@Configuration
@PropertySource("classpath:mail.properties")
public class MailConfiguration {

    @Value("${mail.protocol}")
    private String protocol;
    @Value("${mail.host}")
    private String host;
    @Value("${mail.port}")
    private int port;
    @Value("${mail.smtp.socketFactory.port}")
    private int socketPort;
    @Value("${mail.smtp.auth}")
    private boolean auth;
    @Value("${mail.smtp.starttls.enable}")
    private boolean starttls;
    @Value("${mail.smtp.starttls.required}")
    private boolean startlls_required;
    @Value("${mail.smtp.debug}")
    private boolean debug;
    @Value("${mail.smtp.socketFactory.fallback}")
    private boolean fallback;
    @Value("${mail.from}")
    private String from;
    @Value("${mail.username}")
    private String username;
    @Value("${mail.password}")
    private String password;

    @Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

        Properties mailProperties = new Properties();
        mailProperties.put("mail.smtp.auth", auth);
        mailProperties.put("mail.smtp.starttls.enable", starttls);
        mailProperties.put("mail.smtp.starttls.required", startlls_required);
        mailProperties.put("mail.smtp.socketFactory.port", socketPort);
        mailProperties.put("mail.smtp.debug", debug);
        mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        mailProperties.put("mail.smtp.socketFactory.fallback", fallback);

        mailSender.setJavaMailProperties(mailProperties);
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setProtocol(protocol);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        return mailSender;
    }
}

请注意,我的春天服务器是SSL,因此,我停靠465号港口。 SSL使用港口465。 如果你使用487条,你必须使用TLS。

STEP:

在连接之后,选择在上的上进入较安全的地方。

https://www.google.com/ establishings/security/lesssecureapps

STEP:

如果您在《常设仲裁法院》上签字,则从业。 AVAST邮报盾与发送电子邮件发生冲突。 如果你不放弃,你将出现以下错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

Gmail to work with TLS or SSL:

Port for TLS/STARTTLS: 587
Port for SSL: 465

两者均为单体字码:javax.net.ssl.SSLSocketFactory,mail.smtp.socketFactory.fallback,并制成mail.smtp.starttls.enable

<beans>
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host"><value>smtp.gmail.com</value></property>
        <property name="port"><value>465</value></property>
        <property name="protocol"><value>smtp</value></property>
        <property name="username"><value>XXXXX@gmail.com</value></property>
        <property name="password"><value>XXXX</value></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">false</prop>
                <prop key="mail.smtp.quitwait">false</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                <prop key="mail.smtp.socketFactory.fallback">false</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>
    <bean id="mailMail" class="com.embitel.service.email.EmailService">
        <property name="mailSender" ref="mailSender" />
        <property name="simpleMailMessage" ref="customeMailMessage" />
    </bean>
    <bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="XXXX@gmail.com" />
        <property name="to" value="yyyyy@gmail.com" />
        <property name="subject" value="Testing Subject Line for email senind.." />
        <property name="text">
            <value>
        <![CDATA[
            Dear %s,
            Mail Content : %s
        ]]>
            </value>
        </property>
    </bean>
</beans>

!

在我发言之后,下文(Yaml格式)为我工作。


spring.mail:
  host: smtp.gmail.com
  port: 465
  protocol: smtp
  username: xyz@gmail.com
  password: abc
  test-connection: true
  properties:
    "mail.smtp.auth": true
    "mail.smtp.starttls.enable": true
    "mail.smtp.starttls.required": true
    "mail.smtp.socketFactory.class": javax.net.ssl.SSLSocketFactory
    "mail.debug": true

邮局唯一需要的财产

<prop key="mail.smtp.starttls.enable">true</prop>

Sometime/first time google prevent the sign in to your account by any third party application or using your code. when you will sign in to your account in browser, you will get a message "Google prevents a Suspicious attempt to sign in to your account" see the screenshot below. enter image description here

点击“你”并允许签字。

在这里,我为我工作了:

    public JavaMailSender getJavaMailSender()
    {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setProtocol("smtp");
        sender.setHost("smtp.gmail.com");
        sender.setPort(587);
        sender.setUsername("username");
        sender.setPassword("password");

        Properties mailProps = new Properties();
        mailProps.put("mail.smtps.auth", "true");
        mailProps.put("mail.smtp.starttls.enable", "true");
        mailProps.put("mail.smtp.debug", "true");

        sender.setJavaMailProperties(mailProps);

        return sender;
    }

我认为,你需要利用587港从事小规模作业。

http://www.ohchr.org。

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="465" />
    <property name="protocol" value="smtps" />
    <property name="username" value="my_email@domain.tld" />
    <property name="password" value="my_password" />
    <property name="javaMailProperties">
    <props>
        <prop key="mail.smtps.auth">true</prop>
    </props>
    </property>
</bean>

见谷歌对进一步信息的支持:

这似乎有很大不同,但也许会试图:

<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
    <property name="username" value="XXX@gmail.com" />
    <property name="password" value="XXX" />
    <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.user" value="XXX@gmail.com" />
        <prop key="mail.smtp.password" value="XXX" />
        <prop key="mail.smtp.host">smtp.gmail.com</prop>
        <prop key="mail.smtp.port">587</prop>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
    </property>
</bean>

您应具体说明您的“来自”地址,即<prop key=”mail.smtp.from”>XXX@gmail.com</prop>,或当发出电文时。

我已经回答了你的问题。

how to Implements an async email service in Spring

请注意,这项工作于3年春季进行,因此我无法确定春季2。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...