3학년 1학기 네트워크 과제물로 제출했던 구글 smtp 를 통해 메일 보내기 입니다.
github 주소입니다.
https://github.com/choinamki/send_use_smtp_google/tree/master
사용하실 경우 구글 SMTP 사용 허용과 보안 수준이 낮은 앱 사용 허용 옵션을 체크해야 합니다.
from socket import *
import ssl
import base64
#컴퓨터 공학과 201421282 최남기 2018년 5월 17일
#참고 홈페이지
#https://github.com/seanwatson/school-work/blob/master/ece4436a-assignments/asn1/SMTPClient.py#L73
#https://gist.github.com/msyinmei/fccd95933dae95355bde2cef8fe15f0d
#https://support.google.com/a/answer/3726730?hl=ko&ref_topic=1355150
#https://terms.naver.com/entry.nhn?docId=2272016&cid=51207&categoryId=51207&expCategoryId=51207
#https://support.google.com/mail/answer/7126229?hl=ko&visit_id=1-636621534525936162-2166872604&rd=2
# 아이디 패스워드는 보안 문제로 삭제했습니다.
msg = "\r\n I love computer networks!"
endmsg = "\r\n.\r\n"
mailserver = ("smtp.gmail.com",587)
userid = '' ### 아이디 부분
password = '' ### 패스워드 부분
userid = userid.encode()
password = password.encode()
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect(mailserver)
recv = clientSocket.recv(1024)
recv = recv.decode()
print(recv)
if recv[:3] != '220':
print( '220 reply not received from server.')
#send HELO
heloCommand = 'HELO uyom589\r\n'
heloCommand = heloCommand.encode()
clientSocket.send(heloCommand)
recv1 = clientSocket.recv(1024)
recv1 = recv1.decode()
print(recv1)
if recv1[:3] != '250':
print('250 reply not received from server.')
#starttls
startTlsCommand = 'STARTTLS\r\n'
startTlsCommand = startTlsCommand.encode()
clientSocket.send(startTlsCommand)
recv2 = clientSocket.recv(1024)
recv2 = recv2.decode()
print(recv2)
if recv2[:3] != '220':
print('220 reply not received from server')
#ssl socket
wrapsocket = ssl.wrap_socket(clientSocket)
#login
authCommand = 'AUTH LOGIN\r\n'
authCommand = authCommand.encode()
wrapsocket.send(authCommand)
recv3 = wrapsocket.recv(1024)
recv3 = recv3.decode()
print(recv3)
if recv3[:3] != '334':
print('334 reply not received from server')
#id
uid = base64.b64encode(userid)
wrapsocket.send(uid)
wrapsocket.send('\r\n'.encode())
recv4 = wrapsocket.recv(1024)
recv4 = recv4.decode()
print(recv4)
if recv4[:3] != '334':
print('334 reply not received from server')
#password
upassword = base64.b64encode(password)
wrapsocket.send(upassword)
wrapsocket.send('\r\n'.encode())
recv5 = wrapsocket.recv(1024)
recv5 = recv5.decode()
if recv5[:3] != '235':
print('235 reply not received from server')
#Send mailfrom
mailfrom = 'MAIL FROM:<uyom589@gmail.com>\r\n'
wrapsocket.send(mailfrom.encode())
recv6 = wrapsocket.recv(1024)
recv6 = recv6.decode()
print(recv6)
if recv6[:3] != '250':
print('250 reply not received from server.')
#Send rcpt
rcpt = 'RCPT TO:<uyom589@naver.com>\r\n'
wrapsocket.send(rcpt.encode())
recv7 = wrapsocket.recv(1024)
recv7 = recv7.decode()
print(recv7)
if recv7[:3] != '250':
print('250 reply not received from server')
#send data
data = 'DATA\r\n'
wrapsocket.send(data.encode())
recv8 = wrapsocket.recv(1024)
recv8 = recv8.decode()
print(recv8)
if recv8[:3] != '354':
print('354 reply not received from server.')
#send message
wrapsocket.send(msg.encode())
#message end
wrapsocket.send(endmsg.encode())
recv10 = wrapsocket.recv(1024)
recv10 = recv10.decode()
print(recv10)
if recv10[:3] != '250':
print('250 reply not received from sever.')
#send quit
quitmessage = 'QUIT\r\n'
wrapsocket.send(quitmessage.encode())
recv11 = wrapsocket.recv(1024)
recv11 = recv11.decode()
print(recv11)
if recv11[:3] != '221':
print('221 reply not received from server.')
clientSocket.close()
결과 사진입니다.
'공부' 카테고리의 다른 글
셀레니움 - 구글 번역기 사용하기 (0) | 2019.03.22 |
---|---|
텐서플로우 사용한 2개의 층을 가진 CNN 입니다. (0) | 2019.01.15 |
텐서플로우 xor 네트워크 입니다. (0) | 2019.01.15 |
3학년 2학기 리눅스 프로그래밍 과제입니다. (0) | 2019.01.15 |
파이썬을 이용한 웹페이지 긁어오기(크롤러) (0) | 2019.01.15 |