从任何主机发送电子邮件django

时间:2015-06-04 10:50:09

标签: python django email django-models

我已经阅读了很多通过django发送电子邮件的链接。我已经尝试了所有这些但是他们不能工作。我尝试通过python shell发送电子邮件,我得到了' 1'。 - 那么我应该用什么设置让电子邮件工作,我愿意使用任何邮件服务器? - 我正在使用gmail,但我读到它会导致问题,如果我要使用hotmail例如,我是否需要在settings.xml中指定电子邮件密码? - 如何调试此问题?

2 个答案:

答案 0 :(得分:1)

你可以试试yagmail,它应该让它变得容易多了:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Scroll1 extends JPanel {

    private String text;
    private Font f = (new Font("SansSerif", Font.PLAIN, 24));

    private List<TickerTap> tickerTaps;

    public Scroll1(int startX) {
        text = "We are pleased to announce that Actavis has completed the acquisition of Auden Mckenzie, a dynamic and fast growing company focused on the development, licensing and marketing of generic medicines and proprietary brands in the UK.";
        setFont(f);
        setBackground(Color.WHITE);
        tickerTaps = new ArrayList<>(4);

        Timer timer = new Timer(40, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FontMetrics fm = getFontMetrics(getFont());
                if (tickerTaps.isEmpty()) {
                    tickerTaps.add(new TickerTap(text, getWidth()));
                }

                TickerTap taps[] = tickerTaps.toArray(new TickerTap[tickerTaps.size()]);
                int visibleWidth = 0;
                for (TickerTap tap : taps) {
                    tap.move();
                    int messageWidth = fm.stringWidth(tap.getMessage());
                    if (tap.getxPos() <= -messageWidth) {
                        tickerTaps.remove(tap);
                    } else {
                        visibleWidth += tap.getxPos() + messageWidth;
                    }
                }

                while (visibleWidth < getWidth()) {
                    TickerTap last = tickerTaps.get(tickerTaps.size() - 1);
                    int xPos = last.getxPos() + fm.stringWidth(last.getMessage());
                    String message = " * " + text;
                    TickerTap next = new TickerTap(message, xPos);
                    visibleWidth += next.getxPos() + fm.stringWidth(next.getMessage());
                    tickerTaps.add(next);
                }

                repaint();
            }
        });
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        for (TickerTap tap : tickerTaps) {
            FontMetrics fm = g2d.getFontMetrics();
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
            g2d.setColor(Color.black);
            g2d.drawString(tap.getMessage(), tap.getxPos(), y);
        }
    }

    public class TickerTap {

        private String message;
        private int xPos;

        public TickerTap(String message, int xPos) {
            this.message = message;
            this.xPos = xPos;
        }

        public String getMessage() {
            return message;
        }

        public int getxPos() {
            return xPos;
        }

        public void move() {
            xPos -= 2;
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Scrolling Panel");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Scroll1 scrolling = new Scroll1(-100);

                frame.getContentPane().add(scrolling);
                frame.setSize(400, 300);
                frame.setVisible(true);
            }
        });
    }
}

它有很多功能,例如如何更容易发送附件等。

可以在github找到yagmail。

您可能需要先使用pip安装它:

Timer

答案 1 :(得分:0)

这是使用SMTP Lib发送电子邮件的纯Python代码

from threading import Thread
import requests
import time
import smtplib
def email_sender(input_message, email_to, client):
    ''' function to send email '''
    to = email_to
    gmail_user = 'email of sender account'
    gmail_pwd = 'password of sender account'
    smtpserver = smtplib.SMTP("smtp.gmail.com",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(gmail_user, gmail_pwd)
    header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:site down! \n'
    input_message = input_message + client
    msg = header + input_message
    smtpserver.sendmail(gmail_user, to, msg)
    smtpserver.close()