Skip to content

Instantly share code, notes, and snippets.

@HirofumiYashima
Last active August 29, 2015 14:15
Show Gist options
  • Save HirofumiYashima/7152a04acadf19cf381c to your computer and use it in GitHub Desktop.
Save HirofumiYashima/7152a04acadf19cf381c to your computer and use it in GitHub Desktop.
(解決済み:原因はコード1行漏れ)Python から Gmail にメール送信 拒否される件 ~Gmail アカウント設定したのになぜ? ref: http://qiita.com/HirofumiYashima/items/1b24397c2e915658c984
smtp.login(self.username, self.password)
smtp.mail(self.username)
smtp.rcpt(to)
File Edit Options Buffers Tools Python Help
#!/Usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
class sendGmail:
username, password = 'hirofumi.yashima2012@gmail.com', 'XXXXX'
def __init__(self, to, sub, body):
host, port = 'smtp.gmail.com', 465
msg = MIMEText(body)
msg['Subject'] = sub
msg['From'] = self.username
msg['To'] = to
smtp = smtplib.SMTP_SSL(host, port)
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.mail(self.username)
smtp.rcpt(to)
smtp.data(msg.as_string())
smtp.quit()
if __name__ == '__main__':
to = 'hirofumi.yashima2012@hotmail.co.jp'
sub = 'Python smtplib'
body = 'Hello, Python'
sendGmail(to, sub, body)
File Edit Options Buffers Tools Python Help
#!/Usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
class sendGmail:
username, password = 'hirofumi.yashima2012@gmail.com', 'XXXXX'
def __init__(self, to, sub, body):
host, port = 'smtp.gmail.com', 465
msg = MIMEText(body)
msg['Subject'] = sub
msg['From'] = self.username
msg['To'] = to
smtp = smtplib.SMTP_SSL(host, port)
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.mail(self.username)
smtp.rcpt(to)
smtp.data(msg.as_string())
smtp.quit()
if __name__ == '__main__':
to = 'hirofumi.yashima2012@gmail.com'
sub = 'Pythonから日本語初メール'
body = 'やあ! Python!元気かい?\n今日は暑いね〜。でも、明日からまた寒くなるみたいよ。'
sendGmail(to, sub, body)
#!/Usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
class sendGmail:
username, password = 'hirofumi.yashima2012@gmail.com', 'XXXXX'
def __init__(self, to, sub, body):
host, port = 'smtp.gmail.com', 465
msg = MIMEText(body)
msg['Subject'] = sub
msg['From'] = self.username
msg['To'] = to
smtp = smtplib.SMTP_SSL(host, port)
smtp.ehlo()
smtp.login(self.username, self.password)
smtp.rcpt(to)
smtp.data(msg.as_string())
smtp.quit()
if __name__ == '__main__':
to = 'hirofumi.yashima2012@gmail.com'
sub = 'Python smtplib'
body = 'Hello, Python'
sendGmail(to, sub, body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment